gpt4 book ai didi

php - 如何将参数从 Controller 传递到 YII2 中的布局

转载 作者:IT王子 更新时间:2023-10-29 00:00:53 26 4
gpt4 key购买 nike

我想将参数从 Controller 发送到布局(即 main.php)。但是我无法在 main.php 中获取参数

我试过了:

Controller 代码:

$this->render('index',array('param' => $paramValue));

这就是我试图在布局中得到它的方式。主.php

  1. $this->param(与 yii 1 相同)
  2. $参数

但我无法在布局中获取参数值。谁能告诉我该怎么做?

最佳答案

yii\base\View有特价$params属性(property)。

例如,它用于使用 Gii 在默认生成的 CRUD 代码模板中构建面包屑。

你可以在渲染前这样设置:

use Yii;

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

在 Controller 中你可以这样设置:

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

然后它将在 View 中可用(包括主布局):

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

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

您也可以在 official guide 中找到它.

关于php - 如何将参数从 Controller 传递到 YII2 中的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28038912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com