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

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

How to pass param from controller to layout in YII2

phpyii2

提问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

这就是我试图在布局中获得它的方式,即。主文件

  1. $this->param(as in yii 1)
  2. $param
  1. $this->param(如yii 1)
  2. $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.

我想建议你解决这个问题的一些步骤。

  1. pass parameter to view file
  2. Set parameter to view parameter
  3. 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"];  
    }
    
  1. 传递参数查看文件
  2. 设置参数查看参数
  3. 检查参数,如果存在则使用它。

    //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.phpview, not the main.phpone.

那是因为您正在渲染index.php视图,而不是视图main.php

And $paramthis is how you get it in 1.1 version.

$param这就是你如何得到它在1.1版本。

UPD: If you want a param in your main.phplayout declare it in your Controllerclass, and then you will be able to get it $this->paramthis 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->contextfor 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:

我有同样的问题,我解决了:

  1. In my Controller par exemple, I do that

    $this->view->params['typeUser'] = 'Internaut';

  2. in my View

    $this->params['typeUser'] or echo $this->params['typeUser']

  1. 在我的控制器标准示例中,我这样做

    $this->view->params['typeUser'] = 'Internaut';

  2. 在我看来

    $this->params['typeUser'] or echo $this->params['typeUser']