【问题标题】:Routing with Zend Framework 2 Restful WebserviceZend Framework 2 Restful Webservice 路由
【发布时间】:2013-05-03 23:56:07
【问题描述】:

我想通过使用 Zend Framework 2,更准确地说是 2.1.5 来实现一个 RESTful Web 服务。如果我访问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(
         ...

【问题讨论】:

  • 您的YourModule/Controller 文件夹中是否真的有一个RestController 类。如果是这样,您是否将其映射到 module.config.phpcontrollers 数组的 invokables 部分?即,'YourModule\Controller\Rest' => 'YourModule\Controller\RestController',
  • 感谢 Crisp,正如您在我的存储库中看到的那样,我想我已经做到了。还有什么建议吗?

标签: rest routing zend-framework2


【解决方案1】:

你的路由没有定义控制器所属的命名空间,你需要给路由defaults添加__NAMESPACE__

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

【讨论】:

    【解决方案2】:

    您确定类型有效吗?

    type' => 'ZendMvcRouterHttpSegment',
    

    到这里

    type' => 'Segment',
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-09
      • 2014-05-02
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多