Zend Framework 2 RESTful控制器操作

Mai*_*aik 4 php frameworks zend-framework2

经过多次尝试后,我无法让我的休息功能在我的测试应用程序中运行.

我想知道是否有人在Zend FrameWork 2.0.0beta3中有RestfulController类的经验.

我实现了RestfulController抽象类中的方法,让getList()方法回显"Foo",做了一个curl请求得到一些输出,但我一直得到的是一个空白屏幕.

我知道有zend framework 1.x的选项但是对于我的项目,我需要使用2.x.

如果你们中的一个人能给我一些帮助,我将不胜感激!

Kom*_*ang 5

我正在使用相同类型的应用程序,到目前为止它工作得很好

路由:

'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',
    ),
Run Code Online (Sandbox Code Playgroud)

DI别名:

'alias' => array(
    'index' => 'Rest\Controller\IndexController',
    ...
)
Run Code Online (Sandbox Code Playgroud)

在Controller中,您为渲染返回的内容类型取决于您自己的策略,它可以通过不同的方式实现.

在我的情况下,需要能够在各种格式,如响应:php serialize,json,amfxml,我用Zend\Serializer\Adapter连载我的内容,并直接返回响应情况下,任你在控制器动作通过捕获RestfulController的调度事件直接返回,或集中,并通过回调处理程序返回它.

快速概述:

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 ...
    }
}
Run Code Online (Sandbox Code Playgroud)

也不要忘记在应用程序配置中注册您的模块