【问题标题】:Zend Framework 2 routing issueZend Framework 2 路由问题
【发布时间】:2014-08-08 16:10:43
【问题描述】:

我刚开始学习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(
                        ),
                    ),
                ),
            ),
        ),

用户控制器.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 配置有误,但我无法找出正确的解决方案... :(

非常感谢任何帮助!

【问题讨论】:

    标签: php zend-framework2


    【解决方案1】:

    $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(
                    // .....
                ),
            ),
        )
    

    【讨论】:

      【解决方案2】:

      应该是这样的。

      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', ... ) **/
                      // ...
                  ),
              ),
          ),
      

      【讨论】:

      • 所以动态路由不可能做到这一点?用[controller],[action]组合?
      猜你喜欢
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2014-05-02
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多