如何将参数从控制器传递到 YII2 中的布局
我想将参数从控制器发送到布局(即 main.php).但是我无法在 main.php 中获取参数
I want to send a parameter from controller to layout (i.e. main.php). But I am not able to get the param in main.php
我试过:
控制器代码:
$this->render('index',array('param' => $paramValue));
这就是我试图在布局中获得它的方式,即.main.php
And this is how i was trying to get this in layout ie. main.php
$this->param
(如 yii 1)$param
$this->param
(as in yii 1)$param
但是我无法在布局中获取参数值.谁能告诉我怎么做?
But i am not able to get param value in layout. Can anyone tell me how to do this?
推荐答案
yiiaseView 有特殊的 $params 属性.
例如,它用于使用 Gii 在默认生成的 CRUD 代码模板中构建面包屑.
For example it's used for building breadcrumbs in default generated CRUD code templates with Gii.
渲染前可以这样设置:
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 yiiwebView */
echo $this->params['customParam'];
您也可以在官方指南.
相关文章