我有这样的基本路由设置(只剩下相关的部分):
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
你的defaults
子路由在错误的地方,他们应该在options
'child_routes' => array(
'add' => array(
'type' => 'literal',
'options' => array(
'route' => '/add'
// defaults go here
'defaults' => array(
'action' => 'add'
),
),
),
),