Search code examples
phpcakephpcakephp-2.0

CakePHP set redirect parameters using an array


I am writing some simple redirects in CakePHP 2.4.2 and came across one that stumped me a bit.

How can I redirect to another view with the same passedArgs? It seems like it should be simple, but I must be overlooking something. I tried this:

$this->redirect(array_merge(array('action' => 'index',$this->params['named'])));

The debug output seems correct:

array(
'action' => 'index',
(int) 0 => array(
    'this' => 'url',
    'and' => 'stuff'
)
)

My desired outcome is that

view/this:url/and:stuff

redirects to

index/this:url/and:stuff

but now it just sends me to

index/

Not sure what I am missing here, perhaps I have deeper configuration issues - although it's not a terribly complicated app.


Solution

  • For passed params (numeric keys) use array_merge. But since you use named params, which have a string based key, you can leverage PHP basics:

    $this->redirect(array('action' => 'index') + $this->request->params['named']));
    

    This adds all array values from the named params except for action, which is already in use here (and hence should not be overwritten anyway). So the priority is clear, as well.