Skip to the workspace
Pebellslearn by building
Routing  ›  Lesson

View Routes

View Routes 12 minRoute::view maps a URI straight to a Blade view, optionally with fixed data, for pages that need no logic.

laravel / View Routes

Explore each scene at your own pace.

01 · The rule

The rule

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' means resources/views/about.blade.php, and 'pages.rules' means resources/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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.