- Creating Redirects
- Redirecting To Named Routes
- Redirecting To Controller Actions
- Redirecting With Flashed Session Data
Redirect responses are instances of the Illuminate\Http\RedirectResponse
class, and contain the proper headers needed to redirect the user to another URL. There are several ways to generate a RedirectResponse
instance. The simplest method is to use the global redirect
helper:
Route::get('/dashboard', function () {
return redirect('/home/dashboard');
});
Sometimes you may wish to redirect the user to their previous location, such as when a submitted form is invalid. You may do so by using the global back
helper function. Since this feature utilizes the session, make sure the route calling the back
function is using the web
middleware group or has all of the session middleware applied:
Route::post('/user/profile', function () {
// Validate the request...
return back()->withInput();
});
When you call the redirect
helper with no parameters, an instance of Illuminate\Routing\Redirector
is returned, allowing you to call any method on the Redirector
instance. For example, to generate a RedirectResponse
to a named route, you may use the route
method:
return redirect()->route('login');
If your route has parameters, you may pass them as the second argument to the route
method:
// For a route with the following URI: profile/{id}
return redirect()->route('profile', ['id' => 1]);
For convenience, Laravel also offers the global to_route
function:
return to_route('profile', ['id' => 1]);
If you are redirecting to a route with an "ID" parameter that is being populated from an Eloquent model, you may pass the model itself. The ID will be extracted automatically:
// For a route with the following URI: profile/{id}
return redirect()->route('profile', [$user]);
If you would like to customize the value that is placed in the route parameter, you should override the getRouteKey
method on your Eloquent model:
/**
* Get the value of the model's route key.
*
* @return mixed
*/
public function getRouteKey()
{
return $this->slug;
}
You may also generate redirects to controller actions. To do so, pass the controller and action name to the action
method:
use App\Http\Controllers\HomeController;
return redirect()->action([HomeController::class, 'index']);
If your controller route requires parameters, you may pass them as the second argument to the action
method:
return redirect()->action(
[UserController::class, 'profile'], ['id' => 1]
);
Redirecting to a new URL and flashing data to the session are usually done at the same time. Typically, this is done after successfully performing an action when you flash a success message to the session. For convenience, you may create a RedirectResponse
instance and flash data to the session in a single, fluent method chain:
Route::post('/user/profile', function () {
// Update the user's profile...
return redirect('/dashboard')->with('status', 'Profile updated!');
});
You may use the withInput
method provided by the RedirectResponse
instance to flash the current request's input data to the session before redirecting the user to a new location. Once the input has been flashed to the session, you may easily retrieve it during the next request:
return back()->withInput();
After the user is redirected, you may display the flashed message from the session. For example, using Blade syntax:
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif