phprouteszend-framework2zend-routezend-router

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.


Solution

  • 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.