Skip to content

Commit 77b00d2

Browse files
committed
working on redirect docs
1 parent 6a3d81d commit 77b00d2

File tree

4 files changed

+154
-78
lines changed

4 files changed

+154
-78
lines changed

Diff for: documentation.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [Controllers](/docs/{{version}}/controllers)
1919
- [Requests](/docs/{{version}}/requests)
2020
- [Responses](/docs/{{version}}/responses)
21+
- [Redirects](/docs/{{version}}/redirects)
2122
- [Views](/docs/{{version}}/views)
2223
- [Blade Templates](/docs/{{version}}/blade)
2324
- Architecture Foundations

Diff for: redirects.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Diff for: requests.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,15 @@ All cookies created by the Laravel framework are encrypted and signed with an au
238238

239239
#### Attaching Cookies To Responses
240240

241-
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:
241+
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:
242242

243-
return response('Hello World')->withCookie(
243+
return response('Hello World')->cookie(
244244
'name', 'value', $minutes
245245
);
246246

247-
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:
247+
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:
248248

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

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

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

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

261261
<a name="files"></a>
262262
### Files

0 commit comments

Comments
 (0)