2013-05-03 85 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',