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

Sin*_*hti 12 routing url-routing zend-route zend-framework2

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

return $this->redirect()->toRoute('default', array(
                        'controller' => 'admin',
                        'action' =>  'index'
));
Run Code Online (Sandbox Code Playgroud)

目前正在重定向到 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'
                        ));
Run Code Online (Sandbox Code Playgroud)

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

Rid*_*eal 19

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

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

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

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

这将带您到MyAccountController,indexAction,param1 ='updated'和param2 ='1'.但在您的示例中,应使用参数名称"update"和参数值"1"更新操作


mic*_*lbn 7

这适合我.

路线:

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

并从控制器重定向:

return $this->redirect()->toRoute('user_view', array('user_id'=>$user_id));
Run Code Online (Sandbox Code Playgroud)

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