How To Handle Redirects In Laravel
Redirects are needed when users visit routes that they are not permitted to visit or try to perform actions that they are not permitted to perform.
Laravel offers two common ways to handle redirects and send users to where they should be.
You can redirect with the redirect
helper or the Redirect
facade.
In this shot, all examples will use the redirect()
helper rather than the facade. However, they both do the same thing.
redirect()->to()
redirect()->to()
takes in the path to be redirected to; if the path is not found, a 404 page is returned.
You may also pass in a different status code, different headers, and even set whether to use HTTP or HTTPS.
The definition of the to()
method is as follows:
to($path, $status = 302, $headers = [], $secure = null)
Here’s an example of how to redirect unauthenticated users to the login page:
Route::get('/create-post', function () {
if (!Auth::user()) {
return redirect()->to('/login');
}
});
redirect()->route()
redirect()->route()
is the same as the to()
method explained above. The only difference is that it takes in a named route instead of the raw path.
Given that route parameters cannot be written in the route when using named routes, the route()
method accepts a second parameter that has all the route parameters for the route.
The definition of the route()
method is as follows:
route($route, $parameters = [], $status = 302, $headers = [])
Here’s an example of how to redirect to a particular blog post:
Route::get('/create-post', function () {
return redirect()->route('view-post', ['id' => 27]);
});
This example works the same way as it would if we used the to()
method, as shown below:
Route::get('/create-post', function () {
return redirect()->to('/posts/27');
});
redirect()->back()
Other times, you may just want to send the users back to where they came from. redirect()->back()
takes advantage of the fact that Laravel uses sessions and will always have knowledge of a user’s previously viewed page.
The definition of the back()
method is as follows:
back($status = 302, $headers = [], $fallback = false)
Here’s an example of how to redirect back to the previous page:
Route::get('/create-post', function () {
return redirect()->back();
});
This example may be rewritten to use a shortcut, as shown below:
Route::get('/create-post', function () {
return back();
});
redirect->home()
Sometimes (usually after authentication), you will want to redirect users to their dashboard.
You can use the home()
method to redirect users to a path named home
.
The definition of the home()
method is as follows:
home($status = 302)
Here’s an example of how to redirect to the home page:
Route::get('/create-post', function () {
return redirect()->home();
});
This example does the same thing:
Route::get('/create-post', function () {
return redirect()->route('home');
});
redirect()
You don’t always have to use the to()
or route()
methods. The redirect
helper can be used as a shortcut by itself.
Below is an example where the redirect
helper is used to perform the same function as the first example in the to()
heading:
Route::get('/create-post', function () {
return redirect('/login');
});
Setting a route to always redirect
When applications are updated, you may want to change a route because users will be used to the current one and you don’t want to have issues.
Here’s an example where the posts
path is always redirected to the blog
path:
Route::redirect('/posts', '/blog');
redirect()->with()
Say you want to send data from the current route to the next route that you want to redirect to.
The with()
method can be chained on to the other methods to pass this data.
This is especially useful when dealing with forms where you would want to go back to the form, but with an error or success message.
Here is an example where some data is returned:
Route::get('/create-post', function () {
return redirect()->route('view-post')->with('name', 'Zubair');
});
The returned data can be a string, as seen in the example, or an array.
Other methods
redirect->away()
The away()
method is used to redirect to external URLs. For example:
return redirect()->away('https://google.com');
redirect->refresh()
This method refreshes the current page. For instance:
return redirect()->refresh();
redirect->action()
The redirect->action()
method is used to redirect to call another controller method. Here is an example:
return redirect()->action([PostController::class, 'index']);
Summary
Now you know how to redirect users in your Laravel apps.
This article was originally published on The Educative Team and can be read here.
If you have any questions or relevant advice, please get in touch with me to share them.
To read more of my articles or follow my work, you can connect with me on LinkedIn, Twitter, and Github. It’s quick, it’s easy, and it’s free!