View Routes
Lendly has an About page. It is pure HTML: no database, no form, no logic. Writing a closure that only returns a view works, but it is three lines of ceremony around one idea. Laravel has a one-line shortcut for exactly this case.
The rule
No logic, just a page: use Route::view.
The long way:
Route::get('/about', function () {
return view('about');
});The short way does the same thing:
Route::view('/about', 'about');- First argument: the URI.
- Second argument: the view name, so
'about'meansresources/views/about.blade.php, and'pages.rules'meansresources/views/pages/rules.blade.php. - Optional third argument: an array of fixed data for the view.
Route::view('/rules', 'pages.rules', ['maxLoans' => 3]);Inside the view, {{ $maxLoans }} prints 3.
A view route answers GET (and HEAD) requests. It can be named and given middleware like any other route:
Route::view('/help', 'help')->name('help');What it cannot do is run code for this page. The data array is built when the routes file loads, not when someone visits the page, so it cannot depend on the request, the logged-in member or fresh database results.