【问题标题】:The requested URL could not be matched by routing请求的 URL 无法通过路由匹配
【发布时间】:2015-07-21 04:20:34
【问题描述】:

我正在尝试访问 站点名称/news/id/1,但它给了我这个错误。过去 30 分钟我一直在尝试修复它,但我无法弄清楚我做错了什么。

配置:

            //...
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]/id/[/:id]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'id'         => '[0-9]+',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
            'news' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/news/id[/:id]',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'news',
                    ),
                ),
            ),
        ),
    ),
    //...

【问题讨论】:

  • 我以前从未使用过 Zend 路由,但[:controller 看起来不对。你确定它不应该是[:controller]吗?另外,[/:action]id 中的id 在路由中单独做什么?这条路线对我来说只是关闭
  • 应该是/id/然后才是实际的id

标签: php zend-framework routing zend-framework2 url-routing


【解决方案1】:

您的news 路由属于literal 类型。但它应该是Segment 类型。

请参阅http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html#http-route-types 了解不同的路由类型。

【讨论】:

    【解决方案2】:

    我假设您希望这与您的“默认”路线相匹配。由于该路由是您的应用程序路由的子路由,example.com/application/news/id/1 会匹配,但不会匹配example.com/news/id/1。我会摆脱应用程序路由,只使用默认路由。

    【讨论】:

    • 我从application中删除了id路由,但还是不行
    • 在这种情况下,请您将更新后的路由配置添加到您的问题中,以及您希望与您的/news/id/1 URL 匹配的路由信息​​。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    相关资源
    最近更新 更多