Laravel redirect to named route

Laravel offers an easy way to redirect a user to a specific page. By using the redirect() helper you can easily create a redirect like the following:

return redirect('/home');

This will redirect the user to the /home page of your app. But let's say that you had a named route inside of your routes/web.php, like this:

Route::get('home', 'HomeController@index')->name('home');

If you have a named route, you can easily create a redirect, like so:

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

Now, if you ever change the home route to go to the / page instead of the /home URL, you can update the route:

Route::get('/', 'HomeController@index')->name('home');

And the redirect()->route('home');, will still work; however, the redirect('/home') redirect will no longer work. That's the power of using named routes. You can change the actual URL of the route and your redirects will still work.

Back to DevDojo