I am trying to build a simple REST API for a dictionary App. I would like to have two GET
methods that will get you the word by id and query(string). The problem is that the Slim framework routes everything through the first method and ignores the second one. I understand why it does it and I know you can use query string params but I am hoping there would be a way I can accomplish it this way. Thank you for your help.
http://localhost:5000/dictionary_api/words/1
$app->get('/words/:id', function($id) use ($app, $db) {
});
http://localhost:5000/dictionary_api/words/hello
$app->get('/words/:word', function($word) use ($app,$db){
});
You can supply an array of conditions (regex matches) so that the route parameter will only match a certain format. Try the following
$app->get('/words/:id', function($id) use ($app, $db)
{
//
})->conditions(['id' => '[0-9]+']);
This will make the :id
parameter only match numeric values, anything else it shouldn't match and skip onto the next route.