29.07.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

HTTP Responses 96<br />

.<br />

Sometimes you may wish to redirect the user to their previous location, for example, after a form<br />

submission that is invalid. You may do so by using the global back helper function:<br />

.<br />

1 Route::post('user/profile', function () {<br />

2 // Validate the request...<br />

3<br />

4 return back()->withInput();<br />

5 });<br />

Redirecting To Named Routes<br />

When you call the redirect helper with no parameters, an instance of Illuminate\Routing\Redirector<br />

is returned, allowing you to call any method on the Redirector instance. For example, to generate<br />

a RedirectResponse to a named route, you may use the route method:<br />

.<br />

1 return redirect()->route('login');<br />

If your route has parameters, you may pass them as the second argument to the route method:<br />

.<br />

1 // For a route with the following URI: profile/{id}<br />

2<br />

3 return redirect()->route('profile', [1]);<br />

If you are redirecting to a route with an “ID” parameter that is being populated from an Eloquent<br />

model, you may simply pass the model itself. The ID will be extracted automatically:<br />

.<br />

1 return redirect()->route('profile', [$user]);<br />

Redirecting To Controller Actions<br />

You may also generate redirects to controller actions. To do so, simply pass the controller and<br />

action name to the action method. Remember, you do not need to specify the full namespace to

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!