Optional route parameters being enforced in php Slim Framework v4

1k views Asked by At

I'm using slim 4. I may be misunderstanding optional route parameters. My code is below:

public function __invoke(App $app)
{
    $app->post('/login', LoginHandler::class);
    $app->get('/users/{id:\d+}', UserHandler::class);
}

I expected /users/ to be a valid route as would /users/1, but I find /users/ can't resolve, and I get a 404. I am I misinterpreting the word "optional" here. Cant find anything in any of the slim documentation nor on stackoverflow.

I really didn't want to have to go with the following:

$app->get('/user/{id:\d+}', UserHandler::class);
$app->get('/users/', UserHandler::class);

Any help much appreciated.

Thanks

1

There are 1 answers

2
Nigel Ren On BEST ANSWER

Optional parameters need [] (from https://www.slimframework.com/docs/v4/objects/routing.html#optional-segments), so...

$app->get('/users/[{id:\d+}]', UserHandler::class);