2009-08-27 23 views
5

Wraz z wprowadzeniem Zend_Rest_Route w Zend Framework 1.9 (i jego update w wersji 1.9.2) mamy teraz standardowe rozwiązanie RESTful służące do kierowania żądań. Od sierpnia 2009 r. Nie ma przykładów jego użycia, a jedynie podstawowa dokumentacja znajdująca się w przewodniku referencyjnym.Zend Framework 1.9.2+ Zend_Rest_Route Przykłady

Chociaż jest chyba o wiele bardziej proste niż Zakładam, miałem nadzieję tych bardziej kompetentny niż mógłbym podać kilka przykładów ilustrujących użycie Zend_Rest_Controller w scenariuszu gdzie:

  • Niektóre kontrolery (takie jak IndexController .php) działają normalnie
  • Pozostałe działają jako usługi oparte na odpoczynek (json powrocie)

wydaje się, że teraz w pełni automatyzuje JSON Action Helper i optymalizuje json odpowiedzi na wniosek, co jego używać razem z Zend_Rest_Route idealną kombinacją.

Odpowiedz

6

Pojawia się, że był raczej prosty. Przygotowałem szablon Restful Controller za pomocą Zend_Rest_Controller Abstract. Po prostu zastąp wartości zwracane no_results natywnym obiektem php zawierającym dane, które chcesz zwrócić. Komentarze mile widziane.

<?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

Kończy się poprawka nie sprawiają, że w 1.9.2, trzeba przekazać listAction do indeksu (aktualizacja powyżej). –

+0

Wydaje się być planowana na 1.9.3 (szukaj tutaj "odpoczynku" w tekście strony) http://framework.zend.com/issues/browse/ZF/fixforversion/10360 –

0

wielki post, ale bym pomyślał Zend_Rest_Controller będzie trasa wniosek do właściwego działania w odniesieniu do metody HTTP używany. Byłoby zgrabne, gdyby na przykład żądanie POST do http://<app URL>/Restful byłoby automatycznie _forward do postAction.

Przedstawię poniżej inną strategię, ale być może brakuje mi punktu za Zend_Rest_Controller ... proszę o komentarz.

Moja strategia:

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

Cześć, nie patrząc na wynik odpowiedź na stackoverflow tutaj; 0) Myślałem, że opublikowanie odpowiedzi było jedynym sposobem na skomentowanie twojego postu powyżej. Zauważ, że mój okienko z kodem powyżej wygląda dziwnie w formacie odpowiedzi: 0) –

+0

Dzięki człowieku :) Jest zabawne, po wdrożeniu tego wszystkiego skończyło się na powrocie do mniej restrykcyjnego modelu. Właśnie zauważyłem post z projektu Zend o najlepszych praktykach, zamieszczony tutaj także dla innych. –

+1

'Zend_Rest_Controller' robi to wszystko automatycznie. Brakującą częścią jest [inicjalizacja tras REST] (http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.rest). – Andrew