Laravel 5: Performing HTTP Redirects With redirect

April 14, 2018 —John Koster

The redirect helper function is a useful helper function and makes the task of returning HTTP redirects much easier from controller actions or from routes. The $to parameter is defined with a default value of null. When $to is null, the redirect function by default will return an instance of "Illuminate\Routing\Redirector".

#Signature

The signature of the redirect function is:

1function redirect();
2 
3function redirect(
4 $to = null,
5 $status = 302,
6 $headers = [],
7 $secure = null
8);

#Accessing the Redirector Instance Using redirect

The Redirector class has many useful methods to help control the flow of users in your application. This section will not cover each method in detail, but simply shows how they can be used in conjunction with the redirect helper function:

1// Accessing `Redirector` methods using the `redirect` function:
2 
3return redirect()->back();
4return redirect()->home();
5return redirect()->to('/');

#Redirecting to a Given Path

When an argument that is not null is supplied for the $to parameter, the redirect helper function will internally make a call to the to method on the Redirector instance. The function will also pass in all the arguments supplied to redirect as the arguments to its internal to method call. The following lines are equivalent and would have the exact same effects:

1use Illuminate\Support\Facades\Redirect;
2 
3return redirect('/');
4return redirect()->to('/');
5return Redirect::to('/');

An argument supplied to $to should be the URL or URL segment that the user should be redirect to. For example, if we had the following route defined for /:

1Route::get('/', function() {
2 return "Hello, Universe!";
3});

We could redirect the users there using:

1return redirect('/');

The Redirector will generate the full URL path before it does the redirect. The path / would be converted to the following URL (the actual results will differ based on the path and domain name):

1http://laravel.artisan

#Redirecting to Secure URLs

A truth value of true can be supplied as an argument for the $secure parameter to generate secure (HTTPS) URLs when redirecting:

1return redirect('/', 302, [], true);

The URL that would be internally generated and redirected to in the above example would be (the actual URL will differ based on the path and domain name):

1https://laravel.artisan

#Redirecting to Custom URLs

The Redirector does not provide a direct way to add additional information when generated the final URL, except for the $secure parameter. Luckily, the Redirect uses the "Illuminate\Routing\UrlGenerator" under the hood when generating URLs. Specifically, it calls the to method which will check to see if the path supplied is already a valid URL. This means that we can supply or generate URLs to redirect to without having to worry about the URL being rewritten:

1// The URL will not be rewritten by the `Redirector` when redirecting the user.
2return redirect(
3 'http://laravel.artisan/redirect/test?custom=url'
4);

#Changing the HTTP Status Code

To change the status code of the response simply supply an argument for the $status parameter:

1 
2// Redirect with a "301: Moved Permanently" status code.
3return redirect('/', 301);
4 
5// Redirect with a "307: Temporary Redirect" status code.
6return redirect('/', 307);

#Supplying Additional Headers

In order to supply extra headers we must also provide an argument for $status and then supply an argument for $headers:

1// Redirect with a "302: Found" status code and extra headers:
2return redirect('/', 302, [
3 'Cache-Control' => 'no-cache, must-revalidate'
4]);

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.