Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Before writing a closure, ask "does this page need anything computed per request?" If no, use Route::view. If it needs a query, the current user, or input, use a closure or a controller.

Sorting Lendly's pagesStep 1 of 4
  1. About

    Static text. Nothing computed. Route::view('/about', 'about').

The trap is trying to squeeze logic into the data array, such as ['count' => Book::count()]. That query runs whenever the routes file loads, which slows down every page in the app, and once routes are cached the number is frozen at whatever it was when the cache was built.

Drills

Answer fast — trust the rule.

quick drill

Which line shows resources/views/contact.blade.php at /contact?

quick drill

Which Lendly page is a poor fit for Route::view?

quick drill

What does the third argument in Route::view('/rules', 'rules', ['maxLoans' => 3]) do?

quick drill

Can you chain ->name('about') onto a view route?

Remember

  • No logic, just a page: use Route::view.
  • Ask: anything computed per request? No → view route, yes → closure or controller.
  • Trap: the data array is built when routes load, so never put queries in it.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhen a closure is too much

Lendly has an About page and a House Rules page that never change, and a Books page that loads books from the database. Explain which of these should use Route::view, how you would write one of them, and why the Books page should not.