欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Yii2框架視圖(View)操作及Layout的使用方法分析

 更新時(shí)間:2019年05月27日 09:25:26   作者:學(xué)習(xí)筆記666  
這篇文章主要介紹了Yii2框架視圖(View)操作及Layout的使用方法,結(jié)合具體實(shí)例形式分析了Yii2框架視圖操作及布局layout相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Yii2框架視圖(View)操作及Layout的使用方法。分享給大家供大家參考,具體如下:

渲染視圖

1.我們在Default 控制器里做演示

<?php
namespace app\controllers;
use yii\helpers\Url;
use yii\web\Controller;
class DefaultController extends Controller
{
  public function actionIndex()
  {
//    echo Url::toRoute(['index','id'=>11],true);
//    //http://localhost/yiipro/web/default/index?id=11
//
//    echo Url::base();
//    ///yiipro/web
    // 返回視圖
    return $this->render('index');
  }
}

return $this->render('index'); 就是渲染視圖

2.瀏覽器訪問http://localhost/yiipro/web/index.php/default/,發(fā)現(xiàn)報(bào)錯(cuò)了

這里寫圖片描述

views/default/index.php

說明視圖文件要放在views/default 目錄下。

我們?nèi)?chuàng)建視圖

這里寫圖片描述

再次訪問:

這里寫圖片描述

3.如何向視圖傳遞變量

return $this->render('index',['username'=>'張三','age'=>22]);

<p><?php echo \yii\helpers\Html::encode($username); ?></p>
<p><?php echo \yii\helpers\Html::encode($age); ?></p>

這里寫圖片描述

布局layout

1.默認(rèn)布局

這里寫圖片描述

yii\base\Application::$layout = 'main'
app\module\admin\Module::$layout = 'main';

不使用layout,在控制器中加入屬性public $layout = false
或方法中動(dòng)態(tài)修改$this->layout = false。
也可以使用$this->renderPartial()代替$this->render()

2.新建布局

View/layout/default.php

這里寫圖片描述

我們要在Default控制器里使用這個(gè)布局,寫上:

public $layout = 'default';

瀏覽器效果:

這里寫圖片描述

更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論