Home / Snippets / Laravel Route View (Don't create controller only to return view)
Laravel Route View (Don't create controller only to return view) cover

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

162

2 years ago

0 comments

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

Ahmad Aziz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Post Tutorials

Learn trending tutorials for ReactJs, React Native, VueJS, NextJs, Laravel, Tailwind CSS, and Flutter development.
View Tutorials

Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your web projects
View Components

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this