Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set url parameter in redirect in zend framework v2

I have following redirect script in my controller (Zend Framework 2)

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

Currently redirecting to localhost/zf2/public/admin/index

How can I redirect with an extra parameter?

Like:

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

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

I have tried this :

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

But is redirected to localhost/ttacounting/public/admin/index/updated%2F1

like image 709
Sina Miandashti Avatar asked May 18 '12 11:05

Sina Miandashti


3 Answers

Another way is to pass it as a third parameter (tested on ZF3), instead of updating the route:

$this->redirect()->toRoute('dashboard', [], ['query' => ['welcome' => 1]]);
like image 166
Andron Avatar answered Nov 27 '22 18:11

Andron


This one is working example. The route script

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

Then, setting the parameter in module.config.php

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

This will bring you to MyAccountController, indexAction with param1='updated' and param2='1'. But in your example case, the action should be update with parameter name 'update' and parameter value '1'

like image 30
Rid Zeal Avatar answered Nov 27 '22 17:11

Rid Zeal


This works for me.

The route:

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

And the Redirect from the controller:

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

Notice that the Array key in the redirect statement correspond to the route segment: [/:user_id] = 'user_id'=>$user_id

like image 32
michaelbn Avatar answered Nov 27 '22 18:11

michaelbn