0

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

odan
  • 4,757
  • 5
  • 20
  • 49
EJM76
  • 33
  • 5

1 Answers1

1

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

$app->get('/users/[{id:\d+}]', UserHandler::class);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Cant believe i didnt see this. I really need glasses. That page on routing is confusing as the top talks a lot about optional parameters and shows examples with only {} In any case, thats it now working. Thanks so much for taking time to look at my question – EJM76 Feb 27 '21 at 16:28
  • Thanks for flagging this. I've now marked this answer as accepted – EJM76 Mar 02 '21 at 10:12