CakePHP passing arguments in Controller::redirect

32,955

Solution 1

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

Solution 2

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'];
Share:
32,955
trante
Author by

trante

Updated on January 20, 2020

Comments

  • trante
    trante over 4 years

    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"));
    
  • trante
    trante almost 12 years
    Well, that solution will create this: "tools/index/myArgument:12" But I need this: "tools/index/?myArgument=12"
  • Ross
    Ross almost 12 years
    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
  • Zbigniew
    Zbigniew almost 12 years
    @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 ;)
  • Zbigniew
    Zbigniew almost 12 years
    Correct, I didn't knew '?' can be used to do this.
  • trante
    trante almost 12 years
    @des Because I didn't want to change too much routing code for just one case :) :)
  • Quy Le
    Quy Le almost 8 years
    @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');