Support the ongoing development of Laravel.io →
Configuration Requests

I'd like to pass a predefined value from my route as an argument to controller.

Assuming this is my controller file:

class PagesController extends Controller
{
    public function display($page)
    {
        return view("pages.$page");
    }
}

And this is the routes file:

Route::get('/', 'PagesController@display'); // this must pass "home" as controller argument $page
Route::get('/about', 'PagesController@display'); // this must pass "about" as controller argument $page

I wonder what is the smart laravel way to make the first route pass "home" as controller argument (so that pages.home view is rendered) and the second route pass "about" as controller argument (so that pages.about view is rendered).

I am sure this question is pretty trivial and should happen frequently, but somehow I couldn't find a quick solution. What do I overlook? Thanks!

Last updated 3 years ago.
0
Solution
Route::get('about', ['uses' => 'PagesController@display', 'page' => 'about']);

public function display(\Illuminate\Http\Request $request)
{
    $page = $request->route()->getAction()['page'];
    ...
}

One way to pass that hardcoded data via the route.

0

@lagbox - yes it does the job indeed. Thanks a million!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Lewolf lewolf Joined 21 Mar 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.