0
votes

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!

2
you are using the wrong syntax. that is v2... for v3 ... $app->get('/{:token}' ...geggleto

2 Answers

1
votes

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.

0
votes

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
    });