How To Create HTTP Redirects In Laravel with Example

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

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.Laravel has a useful function redirect() to redirect a user to a different page or action, with or without data.

How To Create HTTP Redirects In Laravel with Example

 The simplest method is to use the global redirect helper:

Route::get('dashboard', function () {
    return redirect('home/dashboard');
});
 

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, make sure the route calling the back function is using the web middleware group or has all of the session middleware applied:
 

Route::post('user/profile', function () {
    // Validate the request...

    return back()->withInput();
});
 


Redirecting To Named Routes

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:

return redirect()->route('login');


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

// For a route with the following URI: profile/{id}

return redirect()->route('profile', ['id' => 1]);




Example(1)

1 − Create a demo.blade.php and Implement code as below

resources/views/demo.blade.php.

<!DOCTYPE html>
<html>
<head>
    <title>Example of Redirecting to Named Routes In Laravel</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<h1>Example of Redirecting to Named Routes In Laravel</h1>
</body>
</html>


2 − In routes.php, create route for demo.blade.php file. We have renamed it to testing. We have also set up another route redirect which will redirect the request to the named route testing.

app/Http/routes.php

Route::get('/demo', ['as'=>'testing',function() {
    return view('demo');
}]);

Route::get('redirect',function() {
    return redirect()->route('testing');
});


3 - Check the output using below url
http://localhost/laraveldemoproject/public/demo


 

Populating Parameters Via Eloquent Models
 

If you are redirecting to a route with an "ID" parameter that is being populated from an Eloquent model, you may pass the model itself. The ID will be extracted automatically:

// For a route with the following URI: profile/{id}

return redirect()->route('profile', [$user]);


If you would like to customize the value that is placed in the route parameter, you can specify the column in the route parameter definition (profile/{id:slug}) or you can override the getRouteKey method on your Eloquent model:

/**
* Get the value of the model's route key.
*
* @return mixed
*/
public function getRouteKey()
{
return $this->slug;
}
 

Redirecting To Controller Actions

You may also generate redirects to controller actions. 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:

return redirect()->action('HomeController@index');


If your controller route requires parameters, you may pass them as the second argument to the action method:
return redirect()->action(
    'UserController@profile', ['id' => 1]
);
 

Example(1)

1.Create demoController using below command

php artisan make:controller demoController


2.Open app/Http/Controllers/demoController.php. Implement code as below in demoController
app/Http/Controllers/demoController.php.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;

class demoController extends Controller
{
    public function index() {
        echo "Redirecting to controller's action.";
    }

}


 

3.Create route in app/Http/routes.php. as below created

app/Http/routes.php

Route::get('testing','demoController@index');
Route::get('/demo',function() {
    return redirect()->action('demoController@index');
});


4 − Pass below url in google chrome.

Route::get('testing','demoController@index');
Route::get('/demo',function() {
    return redirect()->action('demoController@index');
});

Related Post