在zend framework v2中的重定向中设置url参数

时间:2012-05-18 11:42:39

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

我的控制器(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

6 个答案:

答案 0 :(得分:19)

这是一个有效的例子。 路线脚本

$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 ='更新'和param2 =' 1'。 但在您的示例中,应使用参数名称更新操作'更新'和参数值' 1'

答案 1 :(得分:7)

这适合我。

路线:

'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));

请注意,redirect语句中的Array键对应于路段: [/:user_id] ='user_id'=> $ user_id

答案 2 :(得分:1)

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

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

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

希望它有所帮助。

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

尝试使用此路线而不是之前的路线

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

答案 5 :(得分:0)

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

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

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