2009-08-27 9 views
5

ज़ेन फ्रेमवर्क 1.9 (और update में 1.9.2 में Zend_Rest_Route) के परिचय के साथ अब हमारे पास रूटिंग अनुरोधों के लिए एक मानक रीस्टफुल समाधान है। अगस्त 200 9 तक इसके उपयोग के कोई उदाहरण नहीं हैं, संदर्भ मार्गदर्शिका में केवल मूल दस्तावेज पाए गए हैं।ज़ेंड फ्रेमवर्क 1.9.2+ Zend_Rest_Route उदाहरण

यह शायद कहीं अधिक सरल से मुझे लगता है, मैं उन अधिक सक्षम की तुलना में मैं कुछ एक परिदृश्य में Zend_Rest_Controller के उपयोग समझाते उदाहरण प्रदान कर सकते हैं उम्मीद कर रही थी जबकि जहां:

  • कुछ (जैसे indexController के रूप में नियंत्रकों .php) सामान्य रूप से काम
  • दूसरों बाकी आधारित सेवाओं के रूप में काम (लौटने json)

यह JSON Action Helper अब पूरी तरह से स्वचालित करता दिखाई देता है और एक अनुरोध के json प्रतिक्रिया का अनुकूलन, जिससे इसकी Zend_Rest_Route के साथ एक आदर्श संयोजन के साथ उपयोग करें।

उत्तर

6

दिखाता है कि यह अपेक्षाकृत सरल था। मैंने Zend_Rest_Controller सार का उपयोग करके एक आरामदायक नियंत्रक टेम्पलेट को एक साथ रखा है। केवल उस डेटा वाले मूल php ऑब्जेक्ट के साथ no_results रिटर्न मानों को प्रतिस्थापित करें जिसे आप लौटा चाहते हैं। टिप्पणियाँ स्वागत है।

<?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 के लिए स्लेटेड होने लगता है (पेज टेक्स्ट में 'आराम' के लिए खोजें :) http://framework.zend.com/issues/browse/ZF/fixforversion/10360 –

0

बढ़िया पोस्ट, लेकिन मैं सोचा होगा Zend_Rest_Controller मार्ग उपयोग किए गए HTTP विधि के संबंध में सही कार्रवाई करने के लिए अनुरोध करेंगे। के लिए अनुरोध _forward से postAction उदाहरण के लिए POST अनुरोध के बाद यह साफ होगा।

मैं आगे बढ़ जाऊंगा और नीचे एक और रणनीति प्रदान करूंगा, लेकिन हो सकता है कि मैं 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

हाय फिर से, स्कोर करने के लिए नहीं देख रहे हैं यहां स्टैक ओवरफ्लो पर उत्तर; 0) मैंने सोचा था कि उत्तर पोस्ट करना आपके पोस्ट पर टिप्पणी करने का एकमात्र तरीका था। आपको लगता है कि ऊपर मेरा कोड फलक उत्तर प्रारूप में बिचिन दिखता है; 0) –

+0

धन्यवाद आदमी :) यह सब कुछ लागू करने के बाद मजाकिया है, हम एक कम-आराम वाले मॉडल पर वापस जा रहे हैं। ज़ेंड के प्रोजेक्ट लीड से सबसे अच्छी प्रथाओं पर एक पोस्ट को देखा, यहां दूसरों के लिए भी पोस्ट किया गया। –

+1

'Zend_Rest_Controller' यह सब स्वचालित रूप से करता है। जो भाग आप गायब हैं वह है [आरईएसटी रूट शुरू करना] (http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.rest)। – Andrew

संबंधित मुद्दे