php 如何将参数从控制器传递到 YII2 中的布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28038912/
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 param from controller to layout in YII2
提问by Maverick
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
我想从控制器发送一个参数到布局(即 main.php)。但我无法在 main.php 中获取参数
I tried:
我试过:
Controller Code:
控制器代码:
$this->render('index',array('param' => $paramValue));
And this is how i was trying to get this in layout ie. main.php
这就是我试图在布局中获得它的方式,即。主文件
$this->param
(as in yii 1)$param
$this->param
(如yii 1)$param
But i am not able to get param value in layout. Can anyone tell me how to do this?
但我无法在布局中获得参数值。谁能告诉我如何做到这一点?
回答by arogachev
yii\base\Viewhas special $paramsproperty.
yii\base\View有特殊的$params属性。
For example it's used for building breadcrumbs in default generated CRUD code templates with Gii.
例如,它用于使用 Gii 在默认生成的 CRUD 代码模板中构建面包屑。
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 also find it in official guide.
您也可以在官方指南中找到它。
回答by msucil
I would like to suggest you some steps for this problem.
我想建议你解决这个问题的一些步骤。
- pass parameter to view file
- Set parameter to view parameter
Check For parameter and If exist then use it.
//in controller method $this->render("view-file-name",["paramName" => "some parameter"]); //in view file for eg: index.php //i'm passing paremeter sent form controller's action to view params. $this->params["paramFromViewFile"] = $paramName; //here $paramName is the parameter we sent from controller's method //access parameter sent from view file if($this->params && !empty($this->params["paramFromViewFile"])) { echo $this->params["paramFromViewFile"]; }
- 传递参数查看文件
- 设置参数查看参数
检查参数,如果存在则使用它。
//in controller method $this->render("view-file-name",["paramName" => "some parameter"]); //in view file for eg: index.php //i'm passing paremeter sent form controller's action to view params. $this->params["paramFromViewFile"] = $paramName; //here $paramName is the parameter we sent from controller's method //access parameter sent from view file if($this->params && !empty($this->params["paramFromViewFile"])) { echo $this->params["paramFromViewFile"]; }
回答by Vick
Thats because you are rendering index.php
view, not the main.php
one.
那是因为您正在渲染index.php
视图,而不是视图main.php
。
And $param
this is how you get it in 1.1 version.
而$param
这就是你如何得到它在1.1版本。
UPD: If you want a param in your main.php
layout declare it in your Controller
class, and then you will be able to get it $this->param
this way.
UPD:如果你想在你的main.php
布局中有一个参数,在你的Controller
类中声明它,然后你就可以通过$this->param
这种方式得到它。
UPD2: Looks like in 2.0 version you need to declare param in a yii\web\View class. And access it via Yii::$app->view->param
.
UPD2:看起来在 2.0 版本中您需要在 yii\web\View 类中声明参数。并通过Yii::$app->view->param
.
回答by onegun
directly calling $param u should get the value, try
直接调用 $param 你应该得到值,试试
in controller declare a $param,
public $param = '';
then in the acction do
$param='haha';
in layout
echo $this->param;
回答by smohadjer
In Yii2 you can access your controller object from a view using $this->context
for example if in your controller you set a parameter like this:
在 Yii2 中,您可以使用$this->context
例如在您的控制器中设置这样的参数从视图访问您的控制器对象:
public $title = 'My custom title';
Then in your layout you can read it like this:
然后在您的布局中,您可以这样阅读:
$this->context->title
It is explained in Yii documentation in section: Accessing Data in Views
它在 Yii 文档中的部分进行了解释:访问视图中的数据
回答by CHRISTOPHE ABEGA
I had a same problem, I resolve that in:
我有同样的问题,我解决了:
In my Controller par exemple, I do that
$this->view->params['typeUser'] = 'Internaut';
in my View
$this->params['typeUser'] or echo $this->params['typeUser']
在我的控制器标准示例中,我这样做
$this->view->params['typeUser'] = 'Internaut';
在我看来
$this->params['typeUser'] or echo $this->params['typeUser']