简体   繁体   English

ZF2子路线无效

[英]ZF2 Child routes doesn't work

I have a simple config and controller in module Blog: 我在Blog模块中有一个简单的配置和控制器:

module.config.php: module.config.php:

return array(
    'controllers'=>array(
        'invokables'=>array(
            'Blog\Controller\Blog'=>'Blog\Controller\BlogController',
        ),
    ),
    'router'=>array(
        'routes'=>array(
            'blog'=>array(
                'type'=>'literal',
                'options'=>array(
                    'route'=>'/blog',
                    'defaults'=>array(
                        'controller'=>'Blog\Controller\Blog',
                        'action'=>'index',
                    ),
                ),
                'may_terminate'=>true,
                'child_routes'=>array(
                    'rss'=>array(
                        'type'=>'literal',
                        'options' => array(
                            'route'=>'/rss',
                            'defaults'=>array(
                                'action'=>'rss',
                            ),
                        ),
                    ),
                )
            )
        )
    ),
);

BlogController.php: BlogController.php:

namespace Blog\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class BlogController extends AbstractActionController
{
    public function indexAction(){
        return new ViewModel(array());
    }

    public function rssAction(){
        return new ViewModel(array());
    }
}

Route /blog works correctly, 路由/博客正常工作,

but /blog/rss - doesn't work 但是/ blog / rss-不起作用

Zend Framework 2 response with error message: Zend Framework 2响应并显示错误消息:

A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
Controller:
    Blog\Controller\Blog
No Exception available

What's wrong? 怎么了? Thanks in advance. 提前致谢。

您没有像在其父级中那样在`blog / rss'路由may_terminate设置为true

The problem is in matchedRouteName. 问题出在matchedRouteName。

With child_routes 与child_routes

protected 'matchedRouteName' => string 'blog/rss' (length=8), 保护的'matchedRouteName'=>字符串'blog / rss'(长度= 8),

without child_routes 没有child_routes

protected 'matchedRouteName' => string 'blog' (length=4) 保护的'matchedRouteName'=>字符串'blog'(长度= 4)

It generate error in my route treatment and redirect to 404 page when I try access /blog/rss. 当我尝试访问/ blog / rss时,它会在我的路由处理中生成错误并重定向到404页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM