Posts Learn Components Snippets Categories Tags Tools new 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

27 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]']);

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)