0

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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));

Comments

0

Maybe better solution will be used persist attribute in Router::connect()?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.