3

我有一个奇怪的问题。我希望有人能帮助我。

我是 Zend 的新手,我正在编写一个 RESTfull API。

当我将 curl 命令作为 POST 方法运行时,它会调用 putAction() 函数。

例如,我正在运行 curl 命令:

curl -X POST http://localhost/ws/user/post -v

这是回应:

* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> POST /ws/user/post HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Mon, 22 Oct 2012 13:37:56 GMT
< Server: Apache/2.2.14 (Ubuntu)
< X-Powered-By: PHP/5.3.2-1ubuntu4.18
< Vary: Accept-Encoding
< Content-Length: 53
< Content-Type: text/html
< 
* Connection #0 to host localhost left intact
"From putAction() updating the requested article"
Method = POST
* Closing connection #0

这是代码:

[...]
public function postAction() {
    echo 'Method = ' . $_SERVER['REQUEST_METHOD'];

    if (!$id = $this->_getParam('id', false)) {
        $this->sendResponse("Bad request. Id is missing", 400);
    }

    $this->sendResponse("From postAction() creating the requested article", 201);
}

public function putAction() {
    echo 'Method = ' . $_SERVER['REQUEST_METHOD'];
    $this->sendResponse("From putAction() updating the requested article");
}
[...]

有任何想法吗 ?


编辑:

我意识到我把这段代码放在我的引导程序中:

public function _initRestRoute() {
    $front = Zend_Controller_Front::getInstance();
    $router = $front->getRouter();

    $restRoute = new Zend_Rest_Route($front, array(), array(
        'ws',
    ));

    $router->addRoute('rest', $restRoute);
}

当我评论它时,它起作用了。

有什么解释吗?谢谢!

4

1 回答 1

1

http://localhost/ws/user/post在使用 Zend_Rest_Route 时,您不应该在您的请求中出现。此路由将 /user/post 解释为参数并将操作从 post 更改为 put。试试http://localhost/ws吧。

参见 Zend/Rest/Route.php 第 208 行。

于 2012-10-24T10:44:07.727 回答