Zend 2简单路由无法正常工作?

时间:2013-04-19 12:54:01

标签: php zend-framework2

我有这样设置的基本路线(只剩下重要部分):

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'
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),

所以我相信路线是:

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

除最后一个/ts/candidate/add

外,一切顺利

我做了一些基本的观点,每个都返回简单的

echo '<action_name>'

action_name是控制者的行动。 但每次当我输入/ts/candidate/add时,我都会从

获得index action
'Main\Controller\CandidateController'

而不是add action。 视图结构如下所示:

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

1 个答案:

答案 0 :(得分:3)

您的子路线defaults位置错误,位于options

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