【问题标题】:Set url parameter in redirect in zend framework v2在zend框架v2的重定向中设置url参数
【发布时间】:2012-05-25 23:46:53
【问题描述】:

我的控制器中有以下重定向脚本(Zend Framework 2)

return $this->redirect()->toRoute('default', array(
                        'controller' => 'admin',
                        'action' =>  'index'
));

当前重定向到localhost/zf2/public/admin/index

如何使用额外的参数进行重定向?

喜欢:

localhost/zf2/public/admin/index/update/1

localhost/zf2/public/admin/index/page/2

我已经试过了:

return $this->redirect()->toRoute('default', array(
                        'controller' => 'admin',
                        'action' =>  'index'
                            'param' => 'updated/1'
                        ));

但是被重定向到localhost/ttacounting/public/admin/index/updated%2F1

【问题讨论】:

    标签: routing url-routing zend-route zend-framework2


    【解决方案1】:

    这对我有用。

    路线:

    'user_view' => array(
        'type'    => 'Segment',
        'options' => array(
            'route' => '/user/view[/:user_id]',
            'defaults' => array(
                'controller' => 'user',
                'action'     => 'viewUser',
            ),
        ),
    ), // End of user_view route
    

    以及来自控制器的重定向:

    return $this->redirect()->toRoute('user_view', array('user_id'=>$user_id));
    

    注意重定向语句中的 Array 键对应路由段: [/:user_id] = 'user_id'=>$user_id

    【讨论】:

    • 非常感谢我今天要检查这个
    【解决方案2】:

    此语句将您重定向到 localhost/leads/edit/112345:

    return $this->redirect()->toRoute('leads', array(
                    'action' => 'edit',
                    'id' => 112345
                ));
    

    id 是我期望在 editAction 中的参数。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      试试这条路线,而不是你以前的路线

      return $this->redirect()->toRoute('default', [
          'controller' => 'admin',
          'action' =>  'index'
          'updated' => '1'
      ]);
      

      【讨论】:

        【解决方案4】:

        这是一个工作示例。 路由脚本

        $this->redirect()->toRoute('myaccount', array(
            'controller' => 'admin',
            'action' =>  'index',
            'param1' =>'updated',
            'param2'=>'1'
        ));
        

        然后,在module.config.php中设置参数

        'myaccount' => array(
            'type' => 'Segment',
            'options' => array(
            'route'    => '/myaccount[/:action][/:param1][/:param2]',
            'defaults' => array(
                    'controller' => 'Main\Controller\MyAccount',
                    'action'     => 'index',
                ),
            ),
        ),
        

        这会将您带到 MyAccountController,indexAction 的 param1='updated' 和 param2='1'。 但在您的示例中,操作应该使用参数名称“更新”和参数值“1”进行更新

        【讨论】:

        • 这行得通。但是,我要注意,您不需要在数组中指定控制器。您在 module.config.php 中设置的路由会为您执行此操作。您只需要操作和正确的参数。
        【解决方案5】:

        我从上述解决方案中尝试了一些,但没有一个对我有用。我带来了以下内容。

        //Return to specific product pricing 
        return $this->redirect()->toRoute('showpricing', array('id' => $form_values['id']));
        
        
          /* show pricing */
                  'showpricing' => array(
                      'type' => 'Zend\Mvc\Router\Http\Segment',
                      'options' => array(
                          'route'   => '/product/showpricing[?id=:id]',
                          'defaults' => array(
                              'controller' => 'Product\Controller\Product',
                              'action'     => 'showpricing',
                          )
                      )
                  ),
        

        希望这会对某人有所帮助。

        【讨论】:

          【解决方案6】:

          另一种方法是将其作为第三个参数传递(在 ZF3 上测试),而不是更新路由:

          $this->redirect()->toRoute('dashboard', [], ['query' => ['welcome' => 1]]);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-06-25
            • 1970-01-01
            • 2012-06-30
            • 1970-01-01
            • 2015-07-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多