Reputation: 97
I'm trying to set an optional parameter on the index route but can't seem to get it working. I would like to have the option to be able to pass a parameter or not. I can't seem to get anything to work. I was expecting something like the following:
$app->get('/(:token)', function ($req, $res){
// Do Something
});
I came across this solution which is exactly what I want to achieve but can't seem to get it working. Any help would be greatly appreciated!
Upvotes: 0
Views: 770
Reputation: 1579
If you want the token
to be optional, set it in route definition like this:
$app->get('/[(:token)]', function ($request, $response, $args) {
// Will respond to both '/' and '/token'
// Token value is accessible from $args argument
});
Note that the placeholder {:token}
is wraped in square brackets, which makes it an optional segment.
You can read more on the topic in Slim3 User Guide.
Upvotes: 1
Reputation: 451
you should use it like this since Version 3 it think
read here: http://www.slimframework.com/docs/objects/router.html#get-route
$app->get('/{:token}', function ($req, $res){
// Do Something
});
Upvotes: 0