with closure

Sometimes we do not need a controller to display a view we just need to define a callable parameter in our routes.

Route::get('/home', function () {
    $pageTitle = "Home";
    return view('/home', compact('pageTitle'));
});

With this callable parameter we can do some logic in our routes or even query our database to fetch some data and then pass to our views.

We can also pass the route with a parameter and access it inside our callable parameter in our route.

Route::get('/profile/detail/{id}', function ($id) {
   echo $id;
});

Last updated