2011-01-27 81 views
1

我正在使用Zend Framework建立一個網站的過程,它可以被稱爲一個API,或者在一個適當的MVC設置中使用它來構建一個網站。Zend_Rest_Controller沒有調用動作

我已經設置了一個Zend_rest_route來檢查URI並確定使用什麼路線。

要調用我們使用API​​:

http://api.domain.com/controller/action/id/1 

和網站,我們稱之爲

http://domain.com/controller/action/id/1 

的想法是,你只需要創建一個包含可使用的所有操作一個控制器爲網站和API,最大限度地減少重寫代碼的需要。

我創建了一個基礎控制器,擴展了Zend_Rest_Controller,然後由所有控制器擴展,以便具有API的基礎功能。

我面臨的問題是,當我使用API​​調用控制器/操作時,操作不會被調用。

當我的var_dump請求對象,我得到如下:

Website - http://domain.com/guestbook/test/id/5 : 

... 
["_params":protected]=> 
    array(4) { 
    ["controller"]=> 
    string(9) "guestbook" 
    ["action"]=> 
    string(4) "test" 
    ["id"]=> 
    string(1) "5" 
    ["module"]=> 
    string(7) "default" 
    } 
... 

-

API - http://api.domain.com/guestbook/test/id/5 : 
... 
    ["_params":protected]=> 
    array(4) { 
    ["controller"]=> 
    string(9) "guestbook" 
    ["action"]=> 
    string(3) "get" 
    ["test"]=> 
    string(2) "id" 
    ["module"]=> 
    string(7) "default" 
    } 
... 

網站調用正確的行動「測試」,但是API調用「獲取」行動,那麼「測試」就成爲第一個參數。

我怎樣才能調用正確的操作?

PHP CODE TO FOLLOW: 

路由器Bootlstrap:

$this->bootstrap('frontController'); 
$frontController = Zend_Controller_Front::getInstance(); 
$restRoute = new Zend_Rest_Route($frontController); 
$frontController->getRouter()->addRoute('default', $restRoute); 

基地Constroller:

abstract class My_Controller_Base extends Zend_Rest_Controller 
{ 

    public function getAction() 
    { 
     $this->getResponse() 
      ->setHttpResponseCode(200); 
    } 

    public function postAction() 
    { 
     $this->getResponse() 
      ->setHttpResponseCode(201); 
    } 

    public function putAction() 
    { 
     $this->getResponse() 
      ->setHttpResponseCode(200); 
    } 

    public function deleteAction() 
    { 
     $this->getResponse() 
      ->setHttpResponseCode(204); 
    } 

} 

最後我留言控制器:

class GuestbookController extends My_Controller_Base 
{ 
    private $mapper; 
    private $model; 

    public function init() 
    { 
     $this->mapper = new Application_Model_GuestbookMapper(); 
     $this->model = new Application_Model_Guestbook(); 
    } 


    public function indexAction() 
    { 
     $this->view->entries = $this->mapper->fetchAll(); 
    } 

    public function testAction() 
    { 
     $test = new Application_Model_Guestbook(); 
     $id = $this->getRequest()->getParam('id', 1); 
     $this->view->entries = $this->mapper->find($id,$test); 
     var_dump($this->view->entries); 
     $this->_helper->viewRenderer->setNoRender(true); 

    } 

}

回答

0

我沒有使用RESTful服務,但我不認爲你可以設置一個動作。

Zend_Rest_Route Behavior Method  URI  Module_Controller::action 
GET  /product/ratings/ Product_RatingsController::indexAction() 
GET  /product/ratings/:id Product_RatingsController::getAction() 
POST /product/ratings Product_RatingsController::postAction() 
PUT  /product/ratings/:id Product_RatingsController::putAction() 
DELETE /product/ratings/:id Product_RatingsController::deleteAction() 
POST /product/ratings/:id?_method=PUT Product_RatingsController::putAction() 
POST /product/ratings/:id?_method=DELETE  Product_RatingsController::deleteAction() 

在您的Module_Controller之後,只有下兩個值將用作鍵值對,其餘值將被截斷。這就是爲什麼你沒有看到轉儲中的參數5。要使用RESTful服務工作,你需要使用:

http://api.domain.com/guestbook/id/5 

如果你需要使它更深,插入模塊:

http://api.domain.com/module/controller/id/5 
+0

感謝您的幫助,考慮到這一點,並在代碼重新尋找後,我們意識到,使用一個合適的,易於使用的MVC結構更重要的是擁有一個完全平靜的API。所以我們只需堅持正常的MVC,獲取數據並顯示上下文切換。 – Stephan 2011-02-08 14:10:15