Set url parameter in redirect in zend framework v2
Asked Answered
F

6

12

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

Falconiform answered 18/5, 2012 at 11:42 Comment(0)
V
19

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'

Visible answered 6/10, 2013 at 1:30 Comment(1)
This works. I would note, however, that you do not need to specify the controller in the array. The route that you setup in module.config.php does this for you. You only need the action and correct params.Buxom
M
7

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

Menchaca answered 19/5, 2012 at 12:57 Comment(1)
Thank you very much im going to check this todayFalconiform
S
3

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]]);
Sphygmograph answered 16/11, 2016 at 9:45 Comment(0)
P
1

This statement redirects you to localhost/leads/edit/112345:

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

id is the parameter I expect in the editAction.

Hope it helps.

Pampas answered 14/11, 2012 at 16:44 Comment(0)
S
0

Try this route instead of your previous one

return $this->redirect()->toRoute('default', [
    'controller' => 'admin',
    'action' =>  'index'
    'updated' => '1'
]);
Shelley answered 18/5, 2013 at 15:43 Comment(0)
S
0

I tried some from the above solutions but none of them were working for me. I came with the following.

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

Hope this will help someone.

Spanish answered 31/10, 2014 at 4:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.