[英]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.