Right now i’m trying to build a basic endpoint api
$app->group('/player', function (Group $group) {
$group->get('', ListPlayers::class);
$group->get('/getPlayer/{id}', GetCandidate::class);
});
It works if I pass an ID, but it’ll throw an exception if it’s just http://localhost/player/getPlayer/
saying
Uncaught Slim\Exception\HttpMethodNotAllowedException: Method not allowed. Must be one of: OPTIONS in
How do I throw my own error without having slim throw the default or redirect the user to a different action?
I guess really the question would be how to handle unknown / undefined routes?
odan
2
Hi! Is this Slim 3 or Slim 4?
The CORS Pre-Flight route is giving you that specific error message. There’s a bunch of things you can do.
You could remove that route if you don’t need it, Slim will then throw a not found exception instead.
You could make the argument optional:
$group->get('/getPlayer[/{id}]', GetCandidate::class);
or you could add a another route for the no argument case:
$group->get('/getPlayer', SomeClass::class);
You could add a catch all get and or post route at the end of your routes file, but this will probably lead to unintended consequences
$app->get(
'/{routes:.*}',
function (Request $request, Response $response) {
throw new HttpNotFoundException($request, 'nope');
return $response;
}
);