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)

183

3 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

Alaz

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 ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this