4

In controller actions to make redirect I use this:

$this->redirect(array('controller' => 'tools', 'action' => 'index'));

or this

$this->redirect('/tools/index');

And when I pass data with redirect I use this:

$this->redirect('tools/index/?myArgument=12');

But I couldn't find how to pass "myargument" by "this-redirect-array" notation.
I don't want to use this because some routing issues:

$this->redirect(array('controller' => 'tools', 'action' => 'index', "myArgument"));

I need something like this:

$this->redirect(array('controller' => 'tools', 'action' => 'index', "?myArgument=12"));
1
  • As Jleagle demonstrates, it is possible to use the array notion to create standard RFC 3986 query strings, but it's generally not a good idea. Is there a particular reason you don't want to use Cake's friendly URL routing? Commented Jun 25, 2012 at 15:34

3 Answers 3

17

Cake does indeed support query arguments using the question mark, like this:

$this->redirect(array(
    'controller' => 'tools', 'action' => 'index', '?' => array(
        'myArgument' => 12
    )
));

http://book.cakephp.org/2.0/en/development/routing.html#reverse-routing

But it would be better to just do, like des said:

$this->redirect(array(
    'controller' => 'tools', 'action' => 'index', 'myArgument' => 12
));
Sign up to request clarification or add additional context in comments.

1 Comment

Correct, I didn't knew '?' can be used to do this.
3

This should work:

$this->redirect(array('controller' => 'tools', 'action' => 'index', 'myArgument' => 12));

Take a look at CakePHP Cookbook - Controller::redirect

Accessing request parameters:

$this->request['myArgument'];
$this->request->myArgument;
$this->request->params['myArgument'];

5 Comments

Well, that solution will create this: "tools/index/myArgument:12" But I need this: "tools/index/?myArgument=12"
any reason why? You can access that parameter using $this->params['named']. Depending on how you pass the extra params depends on the URL that is output. More in the manual
@trante I've edited my answer to show you how this parameter can be accessed. I see no reason to mix CakePHP routes with classic query string. Is there any specific reason for this? @Ross good comment, but question is for CakePHP2 ;)
@des Because I didn't want to change too much routing code for just one case :) :)
@trante simple you can use ? $this->redirect(array('controller' => 'tools', 'action' => 'index', '?' => array('variableName' => 'variableValue'))); and then in action index you can use $variableValue = $this->request->query('variableName');
0

Using this to redirect:

$this->redirect(array('controller' => 'tools', 'action' => 'index', 'myArgument' => 12));

And Router::connectNamed() to router.php to change separator from ":" to "=":

Router::connectNamed(
    array('myArgument' => array('action' => 'index', 'controller' => 'tools')), array('default' => false, 'greedy' => false, 'separator' => '=')

);

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.