php CakePHP 在 Controller::redirect 中传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11177154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:55:26  来源:igfitidea点击:

CakePHP passing arguments in Controller::redirect

phpcakephpredirectcakephp-2.0

提问by trante

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”表示法传递“myargument”。
我不想使用它,因为一些路由问题:

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

I need something like this:

我需要这样的东西:

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

回答by 472084

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

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

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

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

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

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

但最好只是这样做,就像 des 说的:

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

回答by Zbigniew

This should work:

这应该有效:

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

Take a look at CakePHP Cookbook - Controller::redirect

看看CakePHP Cookbook - Controller::redirect

Accessing request parameters:

访问请求参数

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

回答by sondn

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() 到 router.php 将分隔符从“:”更改为“=”:

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

);

);