forked from laravel/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a3d81d
commit 77b00d2
Showing
4 changed files
with
154 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# HTTP Redirects | ||
|
||
- [Creating Redirects](#creating-redirects) | ||
- [Redirecting To Named Routes](#redirecting-named-routes) | ||
- [Redirecting To Controller Actions](#redirecting-controller-actions) | ||
- [Redirecting With Flashed Session Data](#redirecting-with-flashed-session-data) | ||
|
||
<a name="creating-redirects"></a> | ||
## Creating Redirects | ||
|
||
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](/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: | ||
|
||
Route::post('user/profile', function () { | ||
// Validate the request... | ||
|
||
return back()->withInput(); | ||
}); | ||
|
||
<a name="redirecting-named-routes"></a> | ||
## Redirecting To Named Routes | ||
|
||
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]); | ||
|
||
#### Populating Parameters Via Eloquent Models | ||
|
||
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: | ||
|
||
// 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; | ||
} | ||
|
||
<a name="redirecting-controller-actions"></a> | ||
## Redirecting To Controller Actions | ||
|
||
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: | ||
|
||
return redirect()->action('HomeController@index'); | ||
|
||
If your controller route requires parameters, you may pass them as the second argument to the `action` method: | ||
|
||
return redirect()->action( | ||
'UserController@profile', ['id' => 1] | ||
); | ||
|
||
<a name="redirecting-with-flashed-session-data"></a> | ||
## Redirecting With Flashed Session Data | ||
|
||
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: | ||
|
||
Route::post('user/profile', function () { | ||
// Update the user's profile... | ||
|
||
return redirect('dashboard')->with('status', 'Profile updated!'); | ||
}); | ||
|
||
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): | ||
|
||
@if (session('status')) | ||
<div class="alert alert-success"> | ||
{{ session('status') }} | ||
</div> | ||
@endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.