[英]Zend Framework 2 routing issue
我刚刚开始学习ZF2,因此对于这个问题不重要,我深表歉意。 我要实现的是将用户从projectname.loc:8888 / user重定向到projectname.loc:8888 / user / login如果我手动输入projectname.loc:8888 / user / login,则显示的表单没有任何问题。 如果我输入projectname.loc:8888 / user,则会收到以下错误消息:找不到名称为“ login”的路由。
路由设置为modul.config.php是:
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'__NAMESPACE__' => 'TAuth\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'process' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:controller]/[:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
UserController.php:
public function indexAction() {
return $this->redirect()->toRoute('user/login', array('controller'=>'user', 'action'=>'login'));
}
public function loginAction() {
$form = new Login();
return ['form' => $form];
}
我感觉自己的child_routes配置错误,但我找不到正确的解决方案... :(
任何帮助深表感谢!
应该是这样
public function indexAction() {
// it's user/process rather than user/login
return $this->redirect()->toRoute('user/process', array('controller'=>'user', 'action'=>'login'));
}
供您参考,zf2路由将查找路由密钥。
'user' => array( /** toRoute('user', ... ) **/
'type' => 'Literal',
'options' => array(
// ...
),
'may_terminate' => true,
'child_routes' => array(
'process' => array( /** toRoute('user/process', ... ) **/
// ...
),
'some-other-route' => array( /** toRoute('user/some-other-route', ... ) **/
// ...
),
),
),
$this->redirect()->toRoute('user/login')
的路由名称与您的路由配置中的数组键相关 (不是route
参数,因为我认为您将其与之混淆)
因此,您所需要做的就是添加新的“登录”路线。
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'__NAMESPACE__' => 'TAuth\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// Placed before 'process' so it would match first
'login' => array(
'type' => 'Literal',
'options' => array(
'route' => '/login',
'defaults' => array(
// controller & namespace inherited from parent
'action' => 'login',
),
),
'may_terminate' => true,
),
'process' => array(
// .....
),
),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.