Zend Framework 2 路由问题 [英] Zend Framework 2 routing issue
问题描述
我刚刚开始学习 ZF2,所以如果问题不重要,我深表歉意.我想要实现的是将用户从 projectname.loc:8888/user 重定向到 projectname.loc:8888/user/login如果我手动输入 projectname.loc:8888/user/login ,则表单显示没有任何问题.如果我输入 projectname.loc:8888/user,则会收到以下错误消息:找不到名称为登录"的路由.
I have just started learning ZF2, so I apologize if the question is trivial. What I want to achieve is to redirect the user from projectname.loc:8888/user to projectname.loc:8888/user/login If I enter projectname.loc:8888/user/login manually the form is displayed without any issues. If I enter projectname.loc:8888/user then I get the following error message: Route with name "login" not found.
路由设置为 modul.config.php 是:
The route setting is modul.config.php are:
'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:
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 配置,但我无法找出正确的解决方案... :(
I have a feeling that I have the wrong child_routes configuration but I cannot figure out the proper solution... :(
非常感谢任何帮助!
推荐答案
$this->redirect()->toRoute('user/login')
的路由名称涉及路由配置中的 array 键(不是 route
参数,因为我认为你会混淆它)
The route name of the $this->redirect()->toRoute('user/login')
relates to the array keys in you route config (not the route
parameter as I assume your are confusing it with)
所以你需要做的就是添加一个新的登录"路由.
So all you need to do is add in a new '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(
// .....
),
),
)
这篇关于Zend Framework 2 路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!