Access all of Typerocket. Get Pro.
Redirects
( v6 )
- # What Are Redirects
- # Make Redirect
- # To URL
- # To Home
- # To Site
- # To Admin
- # To URL
- # To Route
- # To Page
- # Back
- # Maybe Back
- # With Fields
- # With Message
- # With Data
- # With Errors
- # With Flash
- # Redirect Now
What Are Redirects
The Redirect
object has several methods for making a redirect. For example, you can redirect to a URL on the home page right away using the following code.
tr_redirect()->toURL(home_url('/sample-page/'))->now();
Make Redirect
$redirect = new \TypeRocket\Http\Redirect();
Or,
$redirect = tr_redirect();
To URL
To redirect to a URL, use the toURL()
method.
$redirect->toUrl( home_url('/blog') );
To Home
To redirect to a URL using the home URL, the toHome()
method.
$redirect->toHome( '/blog' );
To Site
To redirect to a URL using the site URL, the toSite()
method.
$redirect->toSite( 'blog' );
To Admin
To redirect to a URL using the admin URL, the toAdmin()
method.
$redirect->toAdmin('themes.php', ['page' => 'theme_options']);
To URL
To redirect to a URL the toUrl()
method.
$redirect->toUrl( 'https://example.com' );
To Route
To redirect to a named Route
with the toRoute()
method.
$redirect->toRoute('users.show', ['id' => 1]);
To Page
To redirect to an admin page created by TypeRocket as a Page
use the toPage()
method.
$resource = 'Seat';
$action = 'edit';
$item_id = 1;
$redirect->toPage($resource, $action, $item_id);
Back
$redirect->back();
Maybe Back
Redirect back if URL is not already set.
$redirect->maybeBack();
With Fields
To redirect with fields, use the withOldFields()
method. This is great for when validation fails, and you need to send the field data back to a form.
$redirect->withOldFields( ['field_name' => 'The field value'] );
To block fields from being returned, use the second argument.
$redirect->withOldFields( ['field_name' => 'The field value'], ['field_name'] );
Get the fields.
// Using this might keep you Form class
// from accessing old field data.
tr_old_fields();
With Message
$redirect->withMessage('You did it!', 'success');
Get the message.
tr_redirect_message();
With Data
$redirect->withData(['my_data' => 'value']);
Get the data.
tr_redirect_data();
With Errors
$redirect->withErrors(['my_error' => 'value']);
Get the data.
tr_redirect_errors();
With Flash
Redirect with WordPresss admin flash message. Works with the TypeRocket REST API. Types include: success
, error
, and warning
.
$type = 'success';
$redirect->withFlash('Message', $type);
Redirect Now
To redirect right away, use the now()
method.
$redirect->now();
Found a typo? Something is wrong in this documentation? Fork and edit it!