|
| 1 | +# HTTP Redirects |
| 2 | + |
| 3 | +- [Creating Redirects](#creating-redirects) |
| 4 | +- [Redirecting To Named Routes](#redirecting-named-routes) |
| 5 | +- [Redirecting To Controller Actions](#redirecting-controller-actions) |
| 6 | +- [Redirecting With Flashed Session Data](#redirecting-with-flashed-session-data) |
| 7 | + |
| 8 | +<a name="creating-redirects"></a> |
| 9 | +## Creating Redirects |
| 10 | + |
| 11 | +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: |
| 12 | + |
| 13 | + Route::get('dashboard', function () { |
| 14 | + return redirect('home/dashboard'); |
| 15 | + }); |
| 16 | + |
| 17 | +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](/docs/{{version}}/session), make sure the route calling the `back` function is using the `web` middleware group or has all of the session middleware applied: |
| 18 | + |
| 19 | + Route::post('user/profile', function () { |
| 20 | + // Validate the request... |
| 21 | + |
| 22 | + return back()->withInput(); |
| 23 | + }); |
| 24 | + |
| 25 | +<a name="redirecting-named-routes"></a> |
| 26 | +## Redirecting To Named Routes |
| 27 | + |
| 28 | +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: |
| 29 | + |
| 30 | + return redirect()->route('login'); |
| 31 | + |
| 32 | +If your route has parameters, you may pass them as the second argument to the `route` method: |
| 33 | + |
| 34 | + // For a route with the following URI: profile/{id} |
| 35 | + |
| 36 | + return redirect()->route('profile', ['id' => 1]); |
| 37 | + |
| 38 | +#### Populating Parameters Via Eloquent Models |
| 39 | + |
| 40 | +If you are redirecting to a route with an "ID" parameter that is being populated from an Eloquent model, you may simply pass the model itself. The ID will be extracted automatically: |
| 41 | + |
| 42 | + // For a route with the following URI: profile/{id} |
| 43 | + |
| 44 | + return redirect()->route('profile', [$user]); |
| 45 | + |
| 46 | +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: |
| 47 | + |
| 48 | + /** |
| 49 | + * Get the value of the model's route key. |
| 50 | + * |
| 51 | + * @return mixed |
| 52 | + */ |
| 53 | + public function getRouteKey() |
| 54 | + { |
| 55 | + return $this->slug; |
| 56 | + } |
| 57 | + |
| 58 | +<a name="redirecting-controller-actions"></a> |
| 59 | +## Redirecting To Controller Actions |
| 60 | + |
| 61 | +You may also generate redirects to [controller actions](/docs/{{version}}/controllers). To do so, pass the controller and action name to the `action` method. Remember, you do not need to specify the full namespace to the controller since Laravel's `RouteServiceProvider` will automatically set the base controller namespace: |
| 62 | + |
| 63 | + return redirect()->action('HomeController@index'); |
| 64 | + |
| 65 | +If your controller route requires parameters, you may pass them as the second argument to the `action` method: |
| 66 | + |
| 67 | + return redirect()->action( |
| 68 | + 'UserController@profile', ['id' => 1] |
| 69 | + ); |
| 70 | + |
| 71 | +<a name="redirecting-with-flashed-session-data"></a> |
| 72 | +## Redirecting With Flashed Session Data |
| 73 | + |
| 74 | +Redirecting to a new URL and [flashing data to the session](/docs/{{version}}/session#flash-data) 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: |
| 75 | + |
| 76 | + Route::post('user/profile', function () { |
| 77 | + // Update the user's profile... |
| 78 | + |
| 79 | + return redirect('dashboard')->with('status', 'Profile updated!'); |
| 80 | + }); |
| 81 | + |
| 82 | +After the user is redirected, you may display the flashed message from the [session](/docs/{{version}}/session). For example, using [Blade syntax](/docs/{{version}}/blade): |
| 83 | + |
| 84 | + @if (session('status')) |
| 85 | + <div class="alert alert-success"> |
| 86 | + {{ session('status') }} |
| 87 | + </div> |
| 88 | + @endif |
0 commit comments