简体   繁体   中英

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.

Cake expects a flat array of parameters, so you need to use array_merge to add in extra arrays to it on the sides. Try this:

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

... or using your original variable $passedArgs :

$this->redirect(array_merge(array('action' => 'index'), $this->passedArgs));

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.

也许更好的解决方案将在Router::connect()使用persist属性?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM