简体   繁体   中英

ZF2 Child routes doesn't work

I have a simple config and controller in module Blog:

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:

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

Zend Framework 2 response with error message:

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.

With child_routes

protected 'matchedRouteName' => string 'blog/rss' (length=8),

without child_routes

protected 'matchedRouteName' => string 'blog' (length=4)

It generate error in my route treatment and redirect to 404 page when I try access /blog/rss.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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