0

我正在考虑使用 Lucarast RESTler (http://luracast.com/products/restler/)

我的 PHP 类有一个名为“solve”的方法,它必须通过 POST 接受一个参数

class Solver
{
  function solve( $request_data )
  {
    ...
  }

如果我简单地将方法命名为“solve”,则无法通过 POST 访问它。我得到404。

POST http://localhost/path/to/my/method 404 (Not Found)

显然我必须将其命名为“postSolve”,这样才有效。或者创建另一个名为“postSolve”的方法,它只调用“solve”。

public function postSolve( $request_data )
{
    return $this->solve( $request_data );
}

但我不能停止认为必须有一种优雅的方式来做到这一点。

我怎样才能随心所欲地调用我的方法,并且仍然可以通过 POST 访问它?

4

2 回答 2

2

自动路由要求您在 api 方法名称前加上getorpostputordelete将其映射到相应的 HTTP 方法/动词

更多关于这个的例子

但是您始终可以在 api 方法上方使用以下格式的 PHPDoc 注释将任何方法映射到 POST

@url POST my/custom/url/:myvar

例如

class CustomPost
{
    /**
     * @url POST custom/:id
     * @url GET custom
     */
    function anyName($id)
    {
        //do something
    }
}
于 2012-08-23T05:43:53.263 回答
1

在过去的几天里,我学到了更多关于 REST 的知识。除了 get、post、put 或 delete 之外,我不需要调用任何方法。

请参考另一个问题: Understanding REST: Verbs, error code, and authentication

“一般来说,当您认为需要更多动词时,实际上可能意味着您的资源需要重新识别。请记住,在 REST 中,您总是对资源或资源集合进行操作。您选择什么作为资源对于您的 API 定义非常重要。”

于 2012-04-26T22:04:56.757 回答