Skip to content

Commit

Permalink
working on redirect docs
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jul 12, 2016
1 parent 6a3d81d commit 77b00d2
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 78 deletions.
1 change: 1 addition & 0 deletions documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [Controllers](/docs/{{version}}/controllers)
- [Requests](/docs/{{version}}/requests)
- [Responses](/docs/{{version}}/responses)
- [Redirects](/docs/{{version}}/redirects)
- [Views](/docs/{{version}}/views)
- [Blade Templates](/docs/{{version}}/blade)
- Architecture Foundations
Expand Down
88 changes: 88 additions & 0 deletions redirects.md
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
10 changes: 5 additions & 5 deletions requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ All cookies created by the Laravel framework are encrypted and signed with an au

#### Attaching Cookies To Responses

You may attach a cookie to an outgoing `Illuminate\Http\Response` instance using the `withCookie` method. You should pass the name, value, and number of minutes the cookie should be considered valid to this method:
You may attach a cookie to an outgoing `Illuminate\Http\Response` instance using the `cookie` method. You should pass the name, value, and number of minutes the cookie should be considered valid to this method:

return response('Hello World')->withCookie(
return response('Hello World')->cookie(
'name', 'value', $minutes
);

The `withCookie` method also accepts a few more arguments which are used less frequently. Generally, these arguments have the same purpose and meaning as the arguments that would be given to PHP's native [setcookie](http://php.net/manual/en/function.setcookie.php) method:
The `cookie` method also accepts a few more arguments which are used less frequently. Generally, these arguments have the same purpose and meaning as the arguments that would be given to PHP's native [setcookie](http://php.net/manual/en/function.setcookie.php) method:

return response('Hello World')->withCookie(
return response('Hello World')->cookie(
'name', 'value', $minutes, $path, $domain, $secure, $httpOnly
);

Expand All @@ -256,7 +256,7 @@ If you would like to generate a `Symfony\Component\HttpFoundation\Cookie` instan

$cookie = cookie('name', 'value', $minutes);

return response('Hello World')->withCookie($cookie);
return response('Hello World')->cookie($cookie);

<a name="files"></a>
### Files
Expand Down
Loading

0 comments on commit 77b00d2

Please sign in to comment.