2013-05-03 90 views
0

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

你可以看到我的源代码在我github上的存储库: 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(
     ... 
+0

你真的有一个'RestController'类在'YourModule/Controller'文件夹。如果是这样,是否将它映射到'module.config.php'中'controllers'数组的'invokables'部分?即'YourModule \ Controller \ Rest'=>'YourModule \ Controller \ RestController',' – Crisp 2013-05-03 14:39:42

+0

感谢Crisp,你可以在我的仓库中看到,我认为我已经完成了这两项工作。还有什么建议吗? – Jochen 2013-05-03 14:59:28

回答

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_-]*' 
       ), 
      ), 
     ), 
0

type' => 'ZendMvcRouterHttpSegment', 

这个

type' => 'Segment',