[英]Zend Framework 2 RESTful controller actions
經過多次嘗試后,我無法讓我的休息功能在我的測試應用程序中運行。
我想知道是否有人在Zend FrameWork 2.0.0beta3中有RestfulController類的經驗。
我實現了RestfulController抽象類中的方法,讓getList()方法回顯“Foo”,做了一個curl請求得到一些輸出,但我一直得到的是一個空白屏幕。
我知道有zend framework 1.x的選項但是對於我的項目,我需要使用2.x.
如果你們中的一個人能給我一些幫助,我將不勝感激!
我正在使用相同類型的應用程序,到目前為止它工作得很好
路由:
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/[:controller[.:format][/:id]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'format' => '(xml|json|sphp|amf)',
'id' => '[1-9][0-9]*',
),
'defaults' => array(
'controller' => 'Rest\Controller\IndexController',
'format' => 'json',
),
DI別名:
'alias' => array(
'index' => 'Rest\Controller\IndexController',
...
)
在Controller中,您為渲染返回的內容類型取決於您自己的策略,它可以通過不同的方式實現。
在我的情況下,它需要能夠以各種格式響應,如: php serialize
, json
, amf
和xml
,我使用Zend\\Serializer\\Adapter
來序列化我的內容並直接返回響應實例,要么直接在控制器中返回它動作或通過捕獲RestfulController的dispatch事件並通過回調處理程序返回它來集中它。
快速概述:
namespace Rest\Controller
{
use Zend\Mvc\Controller\RestfulController;
class IndexController extends RestfulController
{
public function getList()
{
$content = array(
1 => array(
'id' => 1,
'title' => 'Title #1',
),
2 => array(
'id' => 2,
'title' => 'Title #2',
),
);
/**
* You may centralized this process through controller's event callback handler
*/
$format = $this->getEvent()->getRouteMatch()->getParam('format');
$response = $this->getResponse();
if($format=='json'){
$contentType = 'application/json';
$adapter = '\Zend\Serializer\Adapter\Json';
}
elseif($format=='sphp'){
$contentType = 'text/plain';
$adapter = '\Zend\Serializer\Adapter\PhpSerialize';
}
// continue for xml, amf etc.
$response->headers()->addHeaderLine('Content-Type',$contentType);
$adapter = new $adapter;
$response->setContent($adapter->serialize($content));
return $response;
}
// other actions continue ...
}
}
也不要忘記在應用程序配置中注冊您的模塊
考慮看看這些ZF2模塊:
特別是Module.php和config / module.config.php文件可能會有所幫助。
我不知道你是如何使用當前信息實現它的,但Restful應該可以正常使用ZF2。 我有它在beta2工作。
您還可以在GitHub上查看ZF2 Restful Module Skeleton以獲得靈感。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.