简体   繁体   English

使用Yii2将参数传递到主页

[英]Passing parameter to the homepage using Yii2

I created a RegisterController and after a successful registration I would like to forward the user back to the homepage, problem is, how can I pass a parameter to the homepage without using the redirect call to the controller because they display the long urls. 我创建了一个RegisterControllerRegisterController成功后我想将用户转发回主页,问题是,如何在不使用重定向调用控制器的情况下将参数传递给主页 ,因为它们显示了长网址。 I am passing parameter to the homepage so it could display that the user successfully registered on the site. 我将参数传递给主页,以便显示用户在网站上成功注册。

Tips please? 小贴士好吗? Thanks. 谢谢。

yii\\base\\View has special $params property. yii \\ base \\ View具有特殊的$ params属性。

You can set it like this before rendering: 您可以在渲染之前将其设置为:

use Yii;

Yii::$app->view->params['customParam'] = 'customValue';

Inside a controller you can set it like this: 在控制器内部,您可以这样设置:

$this->view->params['customParam'] = 'customValue';

Then it will be available in views (including main layout): 然后它将在视图中可用(包括主要布局):

/* @var $this yii\web\View */

echo $this->params['customParam'];

You can pass array with redirect method like : 您可以使用重定向方法传递数组,如:

  $data = array('data1'=>'data1', 'data2'=>'data2');    
  return $this->redirect(['site/dashboard', 'data' => $data]);

It could be very simple as you are thinking like redirection would be so long url and all. 它可能非常简单,因为你认为重定向将是如此长的网址和所有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM