2009-08-27 7 views
5

Zend Framework 1.9(および1.9.2のupdate)にZend_Rest_Routeが導入されました。これで、ルーティング要求のための標準化されたRESTfulソリューションが完成しました。 2009年8月現在、その使用法の例はなく、リファレンスガイドにある基本的なドキュメントのみです。Zend Framework 1.9.2+ Zend_Rest_Route例

それはおそらくはるかに簡単で、私は仮定よりもですが、私は私がシナリオどこでZend_Rest_Controllerの使用を示すいくつかの例を提供するかもしれないよりも、それらは多くの有能な期待していた:

  • などindexControllerなど一部のコントローラ( .php)
  • その他は、RESTベースのサービスとして動作し正常に動作(返すJSON)

それはJSON Action Helper今、完全に自動化を表示され、リクエストにJSONレスポンスを最適化し、作るその理想的な組み合わせをZend_Rest_Routeと共に使用してください。

答えて

6

それはかなりシンプルだったようです。 Zend_Rest_Controller Abstractを使って、Restful Controllerテンプレートを作成しました。 no_resultsの戻り値を、返されるデータを含むネイティブのPHPオブジェクトで置き換えます。コメント歓迎。

<?php 
/** 
* Restful Controller 
* 
* @copyright Copyright (c) 2009 ? (http://www.?.com) 
*/ 
class RestfulController extends Zend_Rest_Controller 
{ 

    public function init() 
    { 
     $config = Zend_Registry::get('config'); 
     $this->db = Zend_Db::factory($config->resources->db); 
     $this->no_results = array('status' => 'NO_RESULTS'); 
    } 

    /** 
    * List 
    * 
    * The index action handles index/list requests; it responds with a 
    * list of the requested resources. 
    * 
    * @return json 
    */ 
    public function indexAction() 
    { 
     // do some processing... 
     // Send the JSON response: 
     $this->_helper->json($this->no_results); 
    } 
    // 1.9.2 fix 
    public function listAction() { return $this->_forward('index'); } 

    /** 
    * View 
    * 
    * The get action handles GET requests and receives an 'id' parameter; it 
    * responds with the server resource state of the resource identified 
    * by the 'id' value. 
    * 
    * @param integer $id 
    * @return json 
    */ 
    public function getAction() 
    { 
     $id = $this->_getParam('id', 0); 

     // do some processing... 
     // Send the JSON response: 
     $this->_helper->json($this->no_results); 
    } 

    /** 
    * Create 
    * 
    * The post action handles POST requests; it accepts and digests a 
    * POSTed resource representation and persists the resource state. 
    * 
    * @param integer $id 
    * @return json 
    */ 
    public function postAction() 
    { 
     $id = $this->_getParam('id', 0); 
     $my = $this->_getAllParams(); 

     // do some processing... 
     // Send the JSON response: 
     $this->_helper->json($this->no_results); 
    } 

    /** 
    * Update 
    * 
    * The put action handles PUT requests and receives an 'id' parameter; it 
    * updates the server resource state of the resource identified by 
    * the 'id' value. 
    * 
    * @param integer $id 
    * @return json 
    */ 
    public function putAction() 
    { 
     $id = $this->_getParam('id', 0); 
     $my = $this->_getAllParams(); 

     // do some processing... 
     // Send the JSON response: 
     $this->_helper->json($this->no_results); 
    } 

    /** 
    * Delete 
    * 
    * The delete action handles DELETE requests and receives an 'id' 
    * parameter; it updates the server resource state of the resource 
    * identified by the 'id' value. 
    * 
    * @param integer $id 
    * @return json 
    */ 
    public function deleteAction() 
    { 
     $id = $this->_getParam('id', 0); 

     // do some processing... 
     // Send the JSON response: 
     $this->_helper->json($this->no_results); 
    } 
} 
+0

は1.9.2でそれをしなかったの修正を終了し、あなたは、インデックス(上記更新)にlistActionを転送する必要があります。 –

+0

1.9.3と表示されています(ページテキストの「rest」を検索してください) http://framework.zend.com/issues/browse/ZF/fixforversion/10360 –

0

偉大なポストが、私はZend_Rest_Controllerルートに使用するHTTPメソッドに関して正しい行動への要求を思っただろう。 POSTのリクエストがhttp://<app URL>/Restfulになると、自動的に_forwardからpostActionに自動的になります。

私は下に別の戦略を提供しますが、おそらく私はその背後にある点を見逃しています。Zend_Rest_Controller ...コメントしてください。

私の戦略:

class RestfulController extends Zend_Rest_Controller 
{ 

    public function init() 
    { 
    $this->_helper->viewRenderer->setNoRender(); 
    $this->_helper->layout->disableLayout(); 
    } 

    public function indexAction() 
    { 
     if($this->getRequest()->getMethod() === 'POST') 
      {return $this->_forward('post');} 

     if($this->getRequest()->getMethod() === 'GET') 
      {return $this->_forward('get');} 

     if($this->getRequest()->getMethod() === 'PUT') 
      {return $this->_forward('put');} 

     if($this->getRequest()->getMethod() === 'DELETE') 
      {return $this->_forward('delete');} 

     $this->_helper->json($listMyCustomObjects); 

    } 
    // 1.9.2 fix 
    public function listAction() { return $this->_forward('index'); } 

[the rest of the code with action functions] 
+0

もう一度、ここでstackoverflowの答え; 0)私は答えを投稿すると、上記のあなたの記事にコメントする唯一の方法だと思った。私のコードペインを上に見て、答えの形式でbitchinを見てください; 0) –

+0

ありがとうございます:)面白いですが、これをすべて実装した後は、安らかなモデルに戻ってしまいました。 Zendのプロジェクト・リーダーのベスト・プラクティスに関する記事に気づきました。他の人にもここに掲載されました。 –

+1

'Zend_Rest_Controller'はこれを自動的に行います。あなたが欠けている部分は[REST経路の初期化]です(http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.rest)。 – Andrew