Php Restler 3-参数的默认值阻止传入参数的能力
我有一个非常简单的类,它来自Restler网站上给出的示例“Say”类。详情如下:Php Restler 3-参数的默认值阻止传入参数的能力,php,restler,Php,Restler,我有一个非常简单的类,它来自Restler网站上给出的示例“Say”类。详情如下: <?php class Say { function hello($to='world') { return "Hello $to!"; } function hi($to) { return "Hi $to!"; } function nothing($to='the ground') { return "Looks at {$to} quietly bu
<?php
class Say {
function hello($to='world') {
return "Hello $to!";
}
function hi($to) {
return "Hi $to!";
}
function nothing($to='the ground') {
return "Looks at {$to} quietly but without saying a word.";
}
}
返回
嗨,杰克
太好了。问题是,如果您有一个默认值,如“hello”或“nothing”函数,那么您似乎无法再传入参数:
http://localhost/api/say/hello -- WORKS, returns "Hello world!"
http://localhost/api/say/hello/Jack -- FAILS, returns a JSON error of 404
任何帮助都将不胜感激
另一方面,我还注意到,如果不使用带有“hi”的参数(需要将$设为某个值),它也会返回404错误。我不确定这是否是预期的行为,但这似乎是针对此类错误的错误消息。Restler 2的工作方式与您预期的完全相同,对于上述hello方法,它生成以下路由
GET say/hello ⇠ Say::hello()
GET say/hello/{to} ⇠ Say::hello()
但Restler 3只提供一条路线
GET say/hello ⇠ Say::hello()
这是因为智能路由,我们不将可选参数映射到url,所以可选参数可以作为查询字符串传递
对于hi方法,Restler 2将其路由为
GET say/hi ⇠ Say::hi()
GET say/hi/{to} ⇠ Say::hi()
作为Restler 3在哪里
GET say/hi/{to} ⇠ Say::hi()
因此,由于缺少所需参数,使得say/hi
失败
这样做的原因是为了避免歧义。这是解释在这里的
如果希望Restler 3中的所有API都具有Restler 2行为,请将以下内容添加到index.php
Defaults::$smartAutoRouting = false;
如果您只想在方法级或类级关闭智能自动路由,请添加以下php文档注释
@smart-auto-routing false
@smart-auto-routing false