Laravel Redirections - HTTP Redirects Tutorial | W3schools

Named Routes Redirection

You can provide the desired name to a route. For this purpose, you have the liberty to use “as”, which is an array key.

Also Read: Laravel Views

Route::get(‘user/profile’, [‘as’ => ‘profile’, function() { // }]);

Explanation: Here, we have named the user/profile route as profile. You can use any other name which suits your route.

Here is an example to understand this concept.
Step 1: In the very first step, you are required to create a file with the name test.php. Store this file in the path: resources/views/.

           <h1> Example of Redirecting to Named Routes </h1>


Step 2: Make sure you set up the route for your test.php file in the routes.php file. You can find routes.php at app/Http. Here we are renaming the route to testing. We have set up another route for redirecting the request to testing route. This new route has been named as redirect.

Route::get(‘/test’, [‘as’=>’testing’,function()
    return view(‘test2’);

    return redirect()->route(‘testing’);

Step 3: To test the route’s name, paste the URL: http://localhost:8000/redirect into the web server and the final output will appear like the following.

Explanation: Once you execute the mentioned URL, you will be redirected to the test URL: http://localhost:8000/test. But this URL will automatically redirect you to the testing route.

Redirection To Controller Actions
By now, you have clearly understood that we can redirect to the named route. But, you must also know that redirection to controller actions is also possible. This can be done with the help of action() method. For this, you need 2 arguments which are: the name of the action and the controller itself. And in case, there is any other parameter that you want to pass in the action() method then pass it as the second argument.

return redirect()->action(‘[email protected]’,[parameters]);

The following example will help you to understand this concept clearly.
Step 1: Create a controller with the name RedirectController. For the creation of the controller, execute the following command on the command prompt (cmd) or on the terminal.

laravel> php artisan make:controller RedirectController –plain

Step 2: Once you create the controller successfully then include the given code to the RedirectController.php file. You can find this file at app/Http/Controllers/.


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

class RedirectController extends Controller
      public function index()
           echo “Redirecting to controller’s action.”;

Step 3: Now, add the following code to the routes.php file. You can get this file from the route app/Http/.

Route::get(‘rr’,’[email protected]’);
    return redirect()->action(‘[email protected]’);

Step 4: At last, paste the URL: http://localhost:8000/redirectcontroller to the web server and you will be displayed with the following outcome on the web page.

Laravel Redirection images