使用Zend Framework 2路由Restful Webservice

时间:2013-05-03 14:28:44

标签: rest routing zend-framework2

我希望使用Zend Framework 2实现RESTful Web服务,更准确地说是2.1.5。如果我访问http://ehcserver.localhost/rest,我得到404,相应的消息是'rest(解析为无效的控制器类或别名:rest)'。什么地方出了错?

您可以在我的github-repository中查看我的源代码: https://github.com/Jochen1980/EhcServer/blob/master/module/Application/config/module.config.php

路线的定义如下:

return array(
    'router' => array(
        'routes' => array(
            'rest' => array(
                'type' => 'ZendMvcRouterHttpSegment',
                'options' => array(
                'route' => '/:controller[.:formatter][/:id]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id' => '[a-zA-Z0-9_-]*'
                ),
            ),
         ),
         'home' => array(
         ...

2 个答案:

答案 0 :(得分:2)

您的路线未定义控制器所属的命名空间,您需要添加__NAMESPACE__以路由defaults

        'rest' => array(
            'type' => 'ZendMvcRouterHttpSegment',
            'options' => array(
                'route' => '/:controller[.:formatter][/:id]',
                'defaults' => array(
                    // tell the router which namespace :controller belongs to
                    '__NAMESPACE__' => 'Application\Controller',
                ),
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id' => '[a-zA-Z0-9_-]*'
                ),
            ),
        ),

答案 1 :(得分:0)

您确定类型有效吗?

type' => 'ZendMvcRouterHttpSegment',

到这个

type' => 'Segment',