Posts Learn Components Snippets Categories Tags About
/

Laravel Route View (Don't create controller only to return view)

Use route view automatically render view instead of creating a function body or controller just to return the view

Created on Aug 05, 2021

5 views

Sometimes you might be defining a new "get()" route only to return blade view, but in some cases, this can be simplified to use the "view()" method instead.

Using "get()" Method
So instead of using the "get()" method below.
Route::get('/pages/contact-us', function () {
    return view('pages.contact-us');
});

You can use the "view()" method like below.
Using "view()" Method
Route::view('/pages/contact-us', 'pages.contact-us');

By using this way the code is more expressive and straightforward. Do note that if you need to pass parameters then you can pass in as the 3rd parameter.
Route::view('/pages/contact-us', 'pages.contact-us', ['email' => '[email protected]']);

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components

Other Code Snippets

More