国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

YII框架學(xué)習(xí)筆記之命名空間、操作響應(yīng)與視圖操作示例

2024-05-05 00:08:45
字體:
供稿:網(wǎng)友

本文實(shí)例講述了YII框架命名空間、操作響應(yīng)與視圖操作。分享給大家供大家參考,具體如下:

YII基礎(chǔ)準(zhǔn)備

1.命名空間

<?php/****假設(shè)有三個同名的類,輸出的值為A,B,C****/use a/b/c/apple;use d/e/f/apple as bApple;use g/h/i/apple;$app = new apple();//A$app = new bApple();//B$app = new /Apple();//C 調(diào)用的是全局的

2.操作響應(yīng)

<?php namespace app/controllers;use yii/web/Controller;use yii/data/Pagination;use app/models/Country;class CountryController extends Controller{  public function actionIndex()//不叫方法叫操作  {    $request = /YII::$app->request;//能夠獲取到url值    echo $request->get('id',20);//如果沒有傳參可以設(shè)置默認(rèn)值    if($request->isGet) //isPut    {      echo "this is get method";    }    echo $request->userIP;//獲取用戶IP    $res = /YII::$app->response;//獲取響應(yīng)狀態(tài)    $res->statusCode = 404;//人為設(shè)置響應(yīng)狀態(tài)碼    //$res->headers->add('pragma','no-cache');//設(shè)置head不設(shè)置緩存    $res->headers->set('pragma','max-age=5');//設(shè)置head緩存5分?秒鐘    $res->headers->remove('pragma');    //跳轉(zhuǎn)    $res->headers->add("location","http://www.baidu.com");    $this->redirect("http://www.baidu.com",302);    //文件下載    $res->headers->add('content-disposition','attachment;filename="a.jpg"');    $res->sendFile("robots.txt");  }}

3. Yii視圖操作

<?phpnamespace app/controllers;use yii/web/Controller;class HelloController extends Controller{public function actionIndex(){$hellp_str = "hello God!";$data = array();$data["view_hello_str"] = $hello_str;return $this->renderPartial("index",$data);}}?>

views/hello/index.php

$helper_str = "hello world!<script>console.log(111);</script>"
<?phpuse yii/helpers/Html;//轉(zhuǎn)義use yii/helpers/HtmlPurifier;//過濾html<h1><?= Html::encode($view_hello_str);?></h1> <!--Html::encode() 能防止跨站腳本攻擊,轉(zhuǎn)義html標(biāo)簽--><h1><?= HtmlPurifier::process($view_hello_str);?></h1><?= $this->render('_overview') ?><!--在一個視圖中調(diào)用另一個視圖-->

禁用布局

控制器內(nèi)控制:

public $layout=false/'layout'

控制器成員方法內(nèi)控制:

$this->layout=false/'layout'

視圖中選擇布局:

$this->context->layout=false/'layout'

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


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 泰州市| 务川| 青神县| 霍邱县| 弥勒县| 屏东县| 临汾市| 时尚| 福清市| 台北县| 通海县| 巴林右旗| 白水县| 高清| 菏泽市| 班玛县| 平泉县| 长丰县| 洞口县| 富顺县| 仲巴县| 南木林县| 古田县| 武夷山市| 石泉县| 寿宁县| 江北区| 苗栗市| 济宁市| 共和县| 长丰县| 龙泉市| 黔西县| 澜沧| 民丰县| 右玉县| 丰宁| 大同市| 西畴县| 林芝县| 富源县|