YII框架學(xué)習(xí)筆記之命名空間、操作響應(yīng)與視圖操作示例
本文實(shí)例講述了YII框架命名空間、操作響應(yīng)與視圖操作。分享給大家供大家參考,具體如下:
YII基礎(chǔ)準(zhǔn)備
1.命名空間
<?php /****假設(shè)有三個(gè)同名的類,輸出的值為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);//如果沒(méi)有傳參可以設(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視圖操作
<?php namespace 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>"
<?php use yii\helpers\Html;//轉(zhuǎn)義 use yii\helpers\HtmlPurifier;//過(guò)濾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') ?><!--在一個(gè)視圖中調(diào)用另一個(gè)視圖-->
禁用布局
控制器內(nèi)控制:
public $layout=false/'layout'
控制器成員方法內(nèi)控制:
$this->layout=false/'layout'
視圖中選擇布局:
$this->context->layout=false/'layout'
更多關(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ù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- Yii框架視圖、視圖布局、視圖數(shù)據(jù)塊操作示例
- Yii框架的布局文件實(shí)例分析
- PHP的Yii框架中View視圖的使用進(jìn)階
- PHP的Yii框架中創(chuàng)建視圖和渲染視圖的方法詳解
- Yii控制器中操作視圖js的方法
- Yii視圖操作之自定義分頁(yè)實(shí)現(xiàn)方法
- YII視圖整合kindeditor擴(kuò)展的方法
- Yii2框架視圖(View)操作及Layout的使用方法分析
- Yii視圖CGridView實(shí)現(xiàn)操作按鈕定義地址示例
- Yii視圖CGridView列表用法實(shí)例分析
- Yii框架布局文件的動(dòng)態(tài)切換操作示例
相關(guān)文章
php截取字符串函數(shù)substr,iconv_substr,mb_substr示例以及優(yōu)劣分析
php進(jìn)行中文字符串的截取時(shí),會(huì)經(jīng)常用到二個(gè)函數(shù)iconv_substr和mb_substr,對(duì)這二個(gè)函數(shù)應(yīng)該如何選擇呢?參考下本文介紹的例子就明白了。2014-06-06PHP 文本文章分頁(yè)代碼 按標(biāo)記或長(zhǎng)度(不涉及數(shù)據(jù)庫(kù))
PHP文本分頁(yè),按標(biāo)記或者長(zhǎng)度分頁(yè),非傳統(tǒng)的數(shù)據(jù)庫(kù)分頁(yè)。廢話不多說(shuō),客觀如有意可直接看代碼2012-06-06Thinkphp使用Zxing擴(kuò)展庫(kù)解析二維碼內(nèi)容圖文講解
這篇文章主要介紹了Thinkphp使用Zxing擴(kuò)展庫(kù)解析二維碼內(nèi)容圖文講解,圖文步驟講解的很清晰,有需要的同學(xué)可以跟著小編一起來(lái)學(xué)習(xí)下2021-03-03phpstudy2020搭建站點(diǎn)的實(shí)現(xiàn)示例
這篇文章主要介紹了phpstudy2020搭建站點(diǎn)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10php計(jì)算當(dāng)前程序執(zhí)行時(shí)間示例
這篇文章主要介紹了php計(jì)算當(dāng)前程序執(zhí)行時(shí)間示例,需要的朋友可以參考下2014-04-04常見的5個(gè)PHP編碼小陋習(xí)以及優(yōu)化實(shí)例講解
這篇文章主要介紹了常見的5個(gè)PHP編碼小陋習(xí)實(shí)例講解,講解了常見寫法和優(yōu)化方法,看一下是否自己也是這樣寫的呢2021-02-02Codeigniter上傳圖片出現(xiàn)“You did not select a file to upload”錯(cuò)誤解決辦法
這篇文章主要介紹了Codeigniter上傳圖片出現(xiàn)“You did not select a file to upload”的解決辦法,需要的朋友可以參考下2014-06-06Laravel框架中擴(kuò)展函數(shù)、擴(kuò)展自定義類的方法
這篇文章主要介紹了Laravel框架中擴(kuò)展函數(shù)、擴(kuò)展自定義類的方法,非常實(shí)用的技術(shù)文章,需要的朋友可以參考下2014-09-09