2014-08-29 67 views

回答

0

在你config.module.php

'controllers' => array(
    'invokables' => array(
     'My\Controller\ObjectController' => 'My\Controller\ObjectController' 
    ) 
) 

在你的路由器

'object' => array(
    'type' => 'segment', 
    'options' => array(
     'route' => '/objects[/:object_id]', 
     'constraints' => array(
      'object_id' => '[0-9]*' 
     ), 
     'defaults' => array(
      'controller' => 'My\Controller\ObjectController' 
     ) 
    ) 
), 

你的控制器

<?php 

namespace My\Controller; 

use Zend\Mvc\Controller\AbstractRestfulController; 

class ObjectController extends AbstractRestfulController { 

    public function get($id) 
    { 
     return new JsonModel(... add Json representation of object with $id ...); 
    } 

    public function getList() 
    { 
     return new JsonModel(... add Json representation of object collection ...); 
    } 


    ... add other methods that are needed ... 

} 

這基本上是它

+0

謝謝,但 http://samsonasik.wordpress.com/2012/10/31/zend-framework-2-step-by-step-create-restful-application/ 得到了本教程..非常有幫助.. 我得到了有些疑問.. 請問你能告訴我這兩個控制器在哪裏連接? 也可以告訴我在Restful Controller或AbstractAction Controller中,我把數據庫連接模型放在哪裏? – Vivek 2014-08-29 08:48:04

+0

他們鏈接到路線。您可以從控制器中的方法中的數據庫中獲取數據,或防止代碼複製在控制器內創建一個新方法,並從其他方法調用它。無論你喜歡什麼。 – Wilt 2014-08-29 09:40:11

+0

你能指定控制器嗎? Restful控制器還是AbstractAction控制器? – Vivek 2014-08-29 11:17:45

相關問題