简体   繁体   English

Slim PHP Index Route可选参数

[英]Slim PHP Index Route Optional Parameter

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! 任何帮助将不胜感激!

If you want the token to be optional, set it in route definition like this: 如果您希望token是可选的,请在路由定义中设置它,如下所示:

$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 . 请注意,占位符{:token}包裹在方括号中,这使其成为可选的segment

You can read more on the topic in Slim3 User Guide . 您可以在Slim3用户指南中阅读有关该主题的更多信息。

you should use it like this since Version 3 it think 您应该这样使用它,因为它认为版本3

read here: http://www.slimframework.com/docs/objects/router.html#get-route 在这里阅读: http : //www.slimframework.com/docs/objects/router.html#get-route

   $app->get('/{:token}', function ($req, $res){
      // Do Something
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM