Search

Laravel Application Helper Function: redirect

November 20, 2016 —John Koster

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

The redirect helper function is a versatile helper function. 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.

#Accessing the Redirector 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<?php
2 
3// Accessing `Redirector` methods using the `redirect` function:
4 
5return redirect()->back();
6return redirect()->home();
7return 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:

1<?php
2 
3use Illuminate\Support\Facades\Redirect;
4 
5return redirect('/');
6return redirect()->to('/');
7return 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 /:

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

We could redirect the users there using:

1<?php
2 
3return 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:

1<?php
2 
3return 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 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<?php
2 
3// The URL will not be rewritten by the `Redirector` when redirecting the user.
4return redirect('http://laravel.artisan/redirect/test?custom=url');

#Changing the HTTP Status Code

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

1<?php
2 
3// Redirect with a "301: Moved Permanently" status code.
4return redirect('/', 301);
5 
6// Redirect with a "307: Temporary Redirect" status code.
7return 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<?php
2 
3// Redirect with a "302: Found" status code and extra headers:
4return redirect('/', 302, [
5 'Cache-Control' => 'no-cache, must-revalidate'
6]);

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.