Complete Beginners guide to Redirects in laravel 5.2

There are three common responses that you’ll return from your controller methods or route Closures: views, redirects, and errors. We’ve already covered views and we’ll cover errors next, but let’s address redirects.

There are two common ways to generate a redirect; we’ll use the Façades here, but you may prefer the global helper. Both are create an instance of Illuminate\Http\RedirectResponse, performing some convenience methods on it, and then returning it; you could do this manually, but you’ll have to do a little more work yourself.

Three ways to return a redirect

Route::get('redirect-with-facade', function () { return Redirect::to('auth/login'); }); Route::get('redirect-with-helper', function () { return redirect()->to('auth/login'); }); Route::get('redirect-with-helper-shortcut', function () { return redirect('auth/login'); });

Note that the redirect() helper exposes the same methods as the Redirect Façade, but it also has a shortcut; if you pass parameters directly to the helper, instead of chaining methods after it, it’s a shortcut to the to() Redirect method.

You’ll notice to(), the most commonly used redirect method, has a first parameter that should be set to the URI that you want to redirect the user to. There are a few other options available, though.

Redirect to

The method signature for the to() method for redirects looks like this:

function to($to = null, $status = 302, $headers = [], $secure = null)

$to is a valid internal URI; $status is the HTTP status (defaulting to 301 FOUND); $headers allows you to define which HTTP headers to send along with your redirect; and $secure allows you to override the default choice of http vs https (which is normally set based on your current request URL).

Redirect route

The route() method is the same as the to() method, but rather than point to a particular URI, it points to a particular route name.
 Redirect route

Route::get('redirect', function () { return Redirect::route('conferences.index'); });

Note that, since some route names require parameters, its parameter order is a little bit different; it has an optional second parameter for the route parameters:

function route($to = null, $parameters = [], $status = 302, $headers = [])
Route::get('redirect', function () { return Redirect::route('', ['conference' => 99]); });

So, using it might look a little like below
 Redirect route with parameters

Redirect back

Because of some of the built-in conveniences of Laravel’s session implementation, your application will always have a knowledge of what the user’s previously-visited page was. That opens up the opportunity for a Redirect::back() redirect, which simply redirects the user to whatever page they came from.

Redirect guest and intended

When a user visits a page they’re currently not authenticated for — for example, visiting a dashboard when their login session has expired — Laravel captures their intended URI and redirects back to it after a successful authentication. This is performed using guest() and intended().

Redirect::guest() is a normal Redirect::to() redirect, except it captures the current URL in a query parameter named “url.intended” for use later. You would use this to redirect a user away from their current URL with the intent for them to return after authentication.

Redirect::intendend() grabs the url.intended query parameter and redirects to it. You would use this after successfully authenticating a user, to redirect them back to their intended URI.

Thankfully, the baked-in Laravel authentication already handles these both for you, but you can use them manually if you’re doing your own authentication.

Other redirect methods

The redirect service provides other methods that are less commonly used, but still available:

home() redirects to a route named home
 refresh() redirects to the same page the user is currently on
 away() allows for redirecting to an external URL without the default URL validation
 secure() is like to() with the secure parameter set to true
 action() allows you to like to a controller and method like this: action(MyController@myMethod)

Redirect with

Originally published at

Like what you read? Give LaravelFeed a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.