![](/img/trans.png)
[英]Zend Framework 2 AbstractRestfulController - Exceptions as JSON
[英]How to Create Simple RESTful Application in Zend Framework 2 using AbstractRestfulController?
我是Web服務的新手,我喜歡使用AbstractRestfulController在Zend Framework 2中創建Web服務。任何人都可以給我一個簡單的例子。 可能只有一個功能。
謝謝。
在你的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 ...
}
基本上就是這樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.