【问题标题】:using camel casing for function name in restler在restler中使用camel case作为函数名
【发布时间】:2011-12-12 19:32:47
【问题描述】:
This is the web service file _ws.php
<?php
/*
 Title: Hello World Example.
 Tagline: Let's say hello!.
 Description: Basic hello world example to get started with Restler 2.0.
 Example 1: GET say/hello returns "Hello world!".
 Example 2: GET say/hello/restler2.0 returns "Hello Restler2.0!".
 Example 3: GET say/hello?to=R.Arul%20Kumaran returns "Hello R.Arul Kumaran!".
 */
require_once '../restler/restler.php';
require_once 'news_class.php';
$r = new Restler();
$r->addAPIClass('news');
$r->handle();

这是带有函数的类文件:

<?php
class news {
    function hello($to='world') {
        return "Hello $to!";
    }

    function listnews($page=0){
        $result = "";

        $xml_doc = new DOMDocument;
        $xml_doc->load('news.xml');

        $xsl_doc = new DOMDocument;
        $xsl_doc->load('news_list.xsl');

        $xsl_proc = new XSLTProcessor();
        $xsl_proc->importStyleSheet($xsl_doc);
        $xsl_proc->setParameter('', 'page', $page);
        $result = $xsl_proc->transformToXml($xml_doc);

        return $result;
    }


} // news -

如果我将 listnews 函数命名为 listNews,则会收到以下错误:

{
  "error": {
    "code": 404,
    "message": "Not Found"
  }
}

只要名称现在混合大小写,它就可以工作。 是不是应该是这样的?有什么办法可以用驼色肠衣吗?

谢谢!

【问题讨论】:

  • 有趣,对我来说没有任何问题。我也想看看这个问题的答案。
  • 我们能看到你在哪里调用这个函数吗?可能是您尝试以全小写形式调用该函数。

标签: php restler


【解决方案1】:

只是为了重新审视这一点,我一直在玩弄代码并且我得到了类似的行为。我的方法的以下命名约定会导致 404 错误:

mysite.com/myclass/testMethod
mysite.com/myclass/TestMethod

以下工作按预期进行

mysite.com/myclass/testmethod
mysite.com/myclass/test_method

有什么方法(我错过了或其他方式)让 Camel Case 工作?

更新:方法名称似乎不会导致错误,例如我可以将它命名为 testMethod 并且仍然以小写形式调用它。

【讨论】:

    【解决方案2】:

    更新@richard_askew 的答案,他是对的。

    当我们在 Restler 2.1.5 或更低版本中使用自动路由时,类名、方法名和参数名在 URL 中必须全部小写

    我们刚刚发布了 Restler 2.1.6 版,它改变了这种行为 :)

    现在类名、方法名和url中指定的参数名在任何情况下(大写或小写或混合)都会匹配正确的方法

    【讨论】:

      猜你喜欢
      • 2023-02-08
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多