Posted by Arjun on Thursday 23rd November 2017

Laravel 5.5 - The Route::view method

Laravel 5.x Laravel 5.5 Route::view

Laravel 5.5 introduced new Router class method called view, which gives the flexibility to render the view file directly without creating a controller or closure. This is not a great big feature, but it simplifies your code and removes a couple of files.

How to use Route::view()

Let's define a route with Route::view, to access contact page. with the use of Route::view, we don't have to define the controller or a closure to render a view, you can define a URI and a path to a view file.

// resources/views/pages/contact.blade.php
Route::view('/contact', 'pages.contact');

How to Pass data to view

You can also pass in an array of variables as 3rd paramter that will be passed to the view

// resources/views/pages/contact.blade.php
Route::view('/contact', 'pages.contact',['heading' => 'Contact Us']);