清華大佬耗費三個月吐血整理的幾百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變量的使用 |
掃碼二維碼 獲取免費視頻學習資料