Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andyg1's avatar

Changing the order of route parameters when passing to the controller method

Assume I have routes:

/list/{bar}
/{foo}/list/{bar}

Where the second is more specific. And I have two controllers, BarController and FooBarController, where the latter extends the former for shared functionality.

Now, my understanding the route parameters is that they are passed to controller methods in the order they appear in the ULR parameters i.e.

for FooBarController@edit the method will look like this:

function edit($foo_param, $bar_param)

what I'd like to do is have this edit method declared in the parent BarControlller only, but that requires something like this:

function edit($foo_param = null, $bar_param)

which won't work - because of the order the parameters, $foo_param will never be null. What I want to do is flip the order of the parameters, so I have behaviour like this:

function edit($bar_param, $foo_param = null)

without changing the URL format.

Is it possible to manipulate the variables before sending them to the controller? Perhaps with a closure or something?

Thanks!

0 likes
5 replies
Dunsti's avatar
Dunsti
Best Answer
Level 6

These are the routes you need:

Route::get('/{foo}/list/{bar}', function($foo, $bar) {
    App::call('\App\Http\Controllers\MyController@list', ['bar' => $bar, 'foo' => $foo]);
});

Route::get('/list/{bar}', function($bar) {
    App::call('\App\Http\Controllers\MyController@list', ['bar' => $bar, 'foo' => null]);
});

And your controller looks like this:

    public function list($bar, $foo = null) {
    //
    }
2 likes
sadegh_rouhani's avatar

@Dunsti

actually in laravel 10 its work:

Route::get('/list/{bar}', function($bar) {
    return App::call('\App\Http\Controllers\MyController@list', ['bar' => $bar, 'foo' => null]);
});
Snapey's avatar

I would declare an edit in each but have them each call a real edit function where the work is done.

andyg1's avatar

@Dunsti this is was what I wanted to do, I just didn't know what the correct syntax was. Much appreciated. Thanks :)

Please or to participate in this conversation.