2010-10-22 166 views
2

我有一個休息控制器的例子即時通訊試圖運行,這讓我頭痛。zend框架休息控制器問題

我的網址我嘗試訪問爲localhost /書籍/編輯/ 1

對於一些奇怪的原因,這條路似乎需要與控制器的getAction而不是editAction的。它會拋出錯誤,指出該對象不存在。

的控制器,

class BooksController extends Zend_Rest_Controller { 

    private $_booksTable; 
    private $_form; 

    public function init() { 
     $bootstrap = $this->getInvokeArg ('bootstrap'); 
     $db = $bootstrap->getResource ('db'); 

     $options = $bootstrap->getOption ('resources'); 
     $dbFile = $options ['db'] ['params'] ['dbname']; 
     if (! file_exists ($dbFile)) { 
      $createTable = "CREATE TABLE IF NOT EXISTS books (
         id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
         name VARCHAR(32) NOT NULL, 
         price DECIMAL(5,2) NOT NULL 
        )"; 
      $db->query ($createTable); 

      $insert1 = "INSERT INTO books (name, price) VALUES ('jQuery in Action', 39.99)"; 
      $insert2 = "INSERT INTO books (name, price) VALUES ('PHP in Action', 45.99)"; 
      $db->query ($insert1); 
      $db->query ($insert2); 
     } 

     $this->_booksTable = new Zend_Db_Table ('books'); 
     $this->_form = new Default_Form_Book(); 
    } 

    /** 
    * The index action handles index/list requests; it should respond with a 
    * list of the requested resources. 
    */ 
    public function indexAction() { 
     $this->view->books = $this->_booksTable->fetchAll(); 
    } 

    /** 
    * The list action is the default for the rest controller 
    * Forward to index 
    */ 
    public function listAction() { 
     $this->_forward ('index'); 
    } 

    /** 
    * The get action handles GET requests and receives an 'id' parameter; it 
    * should respond with the server resource state of the resource identified 
    * by the 'id' value. 
    */ 
    public function getAction() { 
     $this->view->book = $this->_booksTable->find ($this->_getParam ('id'))->current(); 
    } 

    /** 
    * Show the new book form 
    */ 
    public function newAction() { 
     $this->view->form = $this->_form; 
    } 

    /** 
    * The post action handles POST requests; it should accept and digest a 
    * POSTed resource representation and persist the resource state. 
    */ 
    public function postAction() { 
     if ($this->_form->isValid ($this->_request->getParams())) { 
      $this->_booksTable->createRow ($this->_form->getValues())->save(); 
      $this->_redirect ('books'); 
     } else { 
      $this->view->form = $this->_form; 
      $this->render ('new'); 
     } 
    } 

    /** 
    * Show the edit book form. Url format: /books/edit/2 
    */ 
    public function editAction() { 
     var_dump ($this->getRequest()->getParam ('edit')); 
     $book = $this->_booksTable->find ($this->getRequest()->getParam ('id'))->current(); 
     var_dump ($book->toArray()); 
     $this->_form->populate ($book->toArray()); 
     $this->view->form = $this->_form; 
     $this->view->book = $book; 
    } 

    /** 
    * The put action handles PUT requests and receives an 'id' parameter; it 
    * should update the server resource state of the resource identified by 
    * the 'id' value. 
    */ 
    public function putAction() { 
     $book = $this->_booksTable->find ($this->_getParam ('id'))->current(); 
     if ($this->_form->isValid ($this->_request->getParams())) { 
      $book->setFromArray ($this->_form->getValues())->save(); 
      $this->_redirect ('books'); 
     } else { 
      $this->view->book = $book; 
      $this->view->form = $this->_form; 
      $this->render ('edit'); 
     } 
    } 

    /** 
    * The delete action handles DELETE requests and receives an 'id' 
    * parameter; it should update the server resource state of the resource 
    * identified by the 'id' value. 
    */ 
    public function deleteAction() { 
     $book = $this->_booksTable->find ($this->_getParam ('id'))->current(); 
     $book->delete(); 
     $this->_redirect ('books'); 
    } 

} 

自舉是,

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { 
    protected function _initAutoload() { 
     $autoloader = new Zend_Application_Module_Autoloader (array (
      'namespace' => 'Default_', 
      'basePath' => dirname (__FILE__) 
     )); 
     return $autoloader; 
    } 

    protected function _initRestRoute() { 
     $this->bootstrap ('Request'); 
     $front = $this->getResource ('FrontController'); 
     $restRoute = new Zend_Rest_Route ($front, array(), array (
      'default' => array ('books') 
     )); 
     $front->getRouter()->addRoute ('rest', $restRoute); 
    } 

    protected function _initRequest() { 
     $this->bootstrap ('FrontController'); 
     $front = $this->getResource ('FrontController'); 
     $request = $front->getRequest(); 
     if (null === $front->getRequest()) { 
      $request = new Zend_Controller_Request_Http(); 
      $front->setRequest ($request); 
     } 
     return $request; 
    } 

} 

有人能看到什麼可能導致的getAction瀏覽到該鏈接時調用???

回答

3

edit應遵循的標識,所以正確的編輯網址是http://localhost/books/1/edit

+0

這是我得到了我的例子來自http://avnetlabs.com/zend-framework/restful-controllers-with-zend-framework – nixgadgets 2010-10-22 08:52:04

+0

如果你不能這樣做,那麼我怎麼可能使用自定義路線? – nixgadgets 2010-10-22 08:53:31

+0

深入挖掘後,Zend/Rest/Route.php中的「新建」和「編輯」有特殊處理方式 – 2010-10-22 10:34:59