lucasctd's avatar

Optional parameters inside group

For some reason if you write a route inside a group only with a optional parameter, Lumen will return 404. For example:


$router->group(['prefix' => 'client'], function () use ($router) {
       $router->get('[/{id}]', ['as' => 'findClient', 'uses' => 'ClientController@find']);
});

However, if I write:

$router->get('/client[/{id}]', ['as' => 'findClient', 'uses' => 'ClientController@find']);

It works. It also works:

$router->group(['prefix' => 'client'], function () use ($router) {
       $router->get('blabla[/{id}]', ['as' => 'findClient', 'uses' => 'ClientController@find']);
});

I think it's a bug. Have any of you guys faced this problem?

0 likes
8 replies
ftiersch's avatar

Does it work with this?

'/[{id}]'

I could imagine that it might be a problem if there is "nothing" but an optional parameter in the route path.

lucasctd's avatar

@FTIERSCH - If you pass the parameter, it works, but if you don't, you'll get 404.

lucasctd's avatar

@JLRDW - I may try it, but I don't think it will work. I am using Lumen, not Laravel, they use different routers. Have you at least tried it?

jlrdw's avatar

@LUCASCTD - In another framework, I have to pass something. So try passing something that won't be used like a 0.

Then in the method, just have a ternary to handle 0 means nothing.

    public function ptest($a = null)
    {
        $mya = ($a == 0 || isNull($a) ? '' : $a);
    }
lucasctd's avatar

@JLRDW - I am sorry but don't want a workaround, I already have one. I just wanted it to work. Your suggestion would be okay If FastRoute doesn't support optional parameters, but it does support optional parameters, so I want make use of it in groups. I have also opened a issue there.

lucasctd's avatar

@JLRDW - I guess you didn't get my problem. In the link you sent me, the problem reported is about a optional parameter in the middle of the route, which is not supported by FastRoute and it's not the problem I am facing... Lumen's route parameters section states it's totally possible to acchieve what I want. Just look at the "Optional Parameters" and Route Groups sections. Anyway, thx for the effort on trying to help me.

Please or to participate in this conversation.