Laravel 5.5 new feature - Route::view() and Route::redirect() method with example

Laravel 5.5 new feature - Route::view() and Route::redirect() method with example

In this tutorial, I will tell you the new features Route::view() and Route::redirect() helpers added with Laravel 5.5.

Route::view()

Sometime you need to return only a view then you define a route and then return view using view() helpers function.

With the release of Laravel 5.5, You can use the Route::view method that provides a easier shortcut load view for a URI.

Route::view method accepts a URI as a first argument and in second argument you will pass the view name.

Before Laravel 5.5

Before Laravel 5.5, If you need to return view only for specific URI then you were using the following syntax :

  1. Route::get('/',function(){
  2.     return view('dashboard');
  3. });
With Laravel 5.5

Laravel 5.5 provides the shortcut for achieving the above functionality in single line:

Route::view('/','dashboard');
Route::redirect()

Route::redirect method provides the shortcut to redirect from one URI to another URI.

Before Laravel 5.5

Sometime you want to redirect to a specific URI by accessing another URI then you defined the route with particular URI and then redirect to specific URI using the redirect method in following way :

  1. Route::get('/',function(){
  2.     return redirect('/dashboard');
  3. });

In the above example, If a user hits the root url then he will be redirected to the dashboard.

With Laravel 5.5

You can achieve the same functionality by using Route::redirect() method in following way :

Route::redirect('/','dashboard');

Phone: (+91) 8800417876
Noida, 201301