php 如何在yii中在重定向时传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13717228/
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
how to pass parameter on redirect in yii
提问by Abhi
I am using Yii framework for my project;
我正在为我的项目使用 Yii 框架;
I am redirecting page after success of insertion in database to another controller using
我在成功插入数据库后将页面重定向到另一个控制器使用
$this->redirect($this->createUrl('controller/action'));
$this->redirect($this->createUrl('controller/action'));
During the redirection is it possible to pass any parameters just like in render,
在重定向期间是否可以像在渲染中一样传递任何参数,
$this->render('selectRefiner', array('param' => $data)
$this->render('selectRefiner', array('param' => $data)
回答by driver_by
回答by Rahul Pawar
try this:
尝试这个:
Yii::$app->response->redirect(['site/dashboard','id' => 1, 'var1' => 'test']);
回答by bds
You can only pass GET parameters in the Yii 2 redirect(). However, I had a similar situation and I resolved it by using Session storage.
您只能在 Yii 2 中传递 GET 参数redirect()。但是,我遇到了类似的情况,我通过使用会话存储解决了它。
Naturally, you can access current Session via Yii::$app->session. Here is an example of using it in two separate controller actions:
当然,您可以通过Yii::$app->session. 这是在两个单独的控制器操作中使用它的示例:
public function actionOne() {
// Check if the Session is Open, and Open it if it isn't Open already
if (!Yii::$app->session->getIsActive()) {
Yii::$app->session->open();
}
Yii::$app->session['someParameter'] = 'Bool/String/Array...';
Yii::$app->session->close();
$this->redirect(['site/two']);
}
public function actionTwo() {
if (isset(Yii::$app->session['someParameter']) {
$param = Yii::$app->session['someParameter'];
} else {
$param = null;
}
$this->render('two', [
'param' => $param
]);
}
So now you should be able to access $paraminside the twoview.
所以现在您应该能够访问视图$param内部two。
For more information, please refer to the official class documentation.
更多信息请参考官方类文档。
回答by rajaera
To redirect into same action with the all parameters that already have this works for me.
使用已经对我有用的所有参数重定向到相同的操作。
$this->redirect($_PHP['SELF']);

