CakePHP在Controller :: redirect中传递参数

时间:2012-06-24 11:42:30

标签: php cakephp redirect cakephp-2.0

在进行重定向的控制器操作中,我使用它:

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

或者

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

当我使用重定向传递数据时,我使用:

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

但我找不到如何通过“this-redirect-array”表示法传递“myargument”。
我不想使用它,因为一些路由问题:

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

我需要这样的东西:

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

3 个答案:

答案 0 :(得分:16)

Cake确实使用问号支持查询参数,如下所示:

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

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

但这样做会更好,就像des说的那样:

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

答案 1 :(得分:3)

这应该有效:

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

查看CakePHP Cookbook - Controller::redirect

Accessing request parameters

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

答案 2 :(得分:0)

使用此方法重定向:

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

和Router :: connectNamed()到router.php来改变"的分隔符:"到" =":

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

);