kamil
kamil

Reputation: 3512

Zend 2 simple route not working correctly?

I have basic routes set like this (left only revelant part):

return array(
    'controllers' => array(
        'invokables' => array(
            'Main\Controller\Login' => 'Main\Controller\LoginController',
            'Main\Controller\Main' => 'Main\Controller\MainController',
            'Main\Controller\Index' => 'Main\Controller\IndexController',
            'Main\Controller\Candidate' => 'Main\Controller\CandidateController',
        ),
    ),

    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Main\Controller\Index',
                        'action' => 'index',
                    ),
                ),
            ),
            'main' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/ts',
                    'defaults' => array(
                        'controller' => 'Main\Controller\Main',
                        'action' => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'candidates' => array(
                        'type' => 'literal',
                        'options' => array(
                            'route' => '/candidate',
                            'defaults' => array(
                                'controller' => 'Main\Controller\Candidate',
                                'action' => 'index'
                            ),
                        ),
                        'may_terminate' => true,
                        'child_routes' => array(
                            'add' => array(
                                'type' => 'literal',
                                'options' => array(
                                    'route' => '/add'
                                ),
                                'defaults' => array(
                                    'action' => 'add'
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),

So I believe the routes are:

/
/ts
/ts/candidate
/ts/candidate/add

Everything works smoothly except the last one /ts/candidate/add

I made some basic views, each returns simple

echo '<action_name>'

Where action_name is controller's action. But each time, when I enter /ts/candidate/add, I got index action from

'Main\Controller\CandidateController'

instead of add action. View structure looks like this:

view
    -- errror
        -- 404.phtml
        -- index.phtml
    -- layout
        -- layout.phtml
        -- login.phtml
    -- main
        -- candidate
            -- index.phtml
            -- add.phtml
        -- main
            -- index.phtml

Upvotes: 0

Views: 69

Answers (1)

Crisp
Crisp

Reputation: 11447

You have the defaults for the child route in the wrong place, they should be inside options

                    'child_routes' => array(
                        'add' => array(
                            'type' => 'literal',
                            'options' => array(
                                'route' => '/add'
                                // defaults go here
                                'defaults' => array(
                                    'action' => 'add'
                                ),
                            ),
                        ),
                    ),

Upvotes: 3

Related Questions