How to write a route that returns only a view in Laravel

Categorized as Laravel Tagged

You can write a route that only returns a view in your routes/web.php using the Route::view method instead of closure function inside the Route:get method. See the example below:

// ❌ Old way
Route::get('careers', function () {
  return view('pages.careers');
});

// ✅ New way
Route::view('careers', 'pages.careers');

You can also pass additional data to the view with this method:

Route::view('careers', 'pages.careers', [
    'name' => 'Coursecomp'
]);

Leave a reply

Your email address will not be published. Required fields are marked *