清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
|
Controller变量与View页面、Layout面面传递方式 在Yii2.0中,view模板文件里面的$this已经不是对应的控制器对象了,而是View对象的变量。我们可以通过View对象中的params变量来传递数据。 先获取当前View,然后给view设置参数,
class SiteController extends Controller
{
public function actionIndex()
{
//设置当前view的params参数,
$view = Yii::$app->view;
$view->params['layoutData']='test';
return $this->render('index');
}
}
在layouts/main.php
<?php
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use frontend\assets\AppAsset;
use frontend\widgets\Alert;
/**
* @var \yii\web\View $this
* @var string $content
*/
AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>"/>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<?php echo $this->params['layoutData']?>
..............
view里面的$this由控制器对象变为View对象是其中的一个大改变,这样整个框架也更清晰了。 View类中context变量的使用 |
扫码二维码 获取免费视频学习资料

- 本文固定链接: http://www.phpxs.com/j/yii2/1001042/
- 免费: Python视频资料获取