2

我有一个非常简单的类,它源自 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 but without saying a word.";
  }
}

因为“hi”函数没有 $to 变量的默认值,所以它在很大程度上可以正常工作:

http://localhost/api/say/hi/Jack

返回

嗨杰克!

伟大的。问题是当你有一个像“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 错误。我不确定这是否是预期的行为,但这种错误似乎是错误的错误消息。

4

1 回答 1

3

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 doc 注释

@smart-auto-routing false
于 2012-10-11T01:54:27.990 回答