這篇文章將為大家詳細(xì)講解有關(guān)使用Yii 框架怎么創(chuàng)建一個(gè)控制器,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
Yii 框架控制器創(chuàng)建使用
在根目錄下的controllers目錄下創(chuàng)建控制器HelloController.php:
<?php namespace app\controllers; use yii\web\Controller; class HelloController extends Controller{ //方法必須以action開頭 public function actionIndex(){ echo 'hello'; } }
訪問(wèn)地址:basic/web/index.php?r=hello/index
參數(shù)r后邊跟控制器名字/方法名字。
如果需要傳遞參數(shù):
<?php namespace app\controllers; use yii\web\Controller; class HelloController extends Controller{ //方法必須以action開頭 public function actionIndex(){ $request = \YII::$app->request; $id = $request->get('id','');//第二個(gè)參數(shù)是如果沒(méi)有傳遞怎么處理 echo 'hello,id=' . $id; //判斷是否是get或post請(qǐng)求 if($request->isGet){ echo 'get'; } if($request->isPost){ echo 'post'; } //獲取用戶地址 echo $request->userIP; } }
Yii 框架控制器響應(yīng)
public function actionIndex(){ //控制器響應(yīng)處理 $res = \Yii::$app->response; //設(shè)置狀態(tài)碼 // $res->statusCode = '404'; //設(shè)置header頭 // $res->headers->add('pragma','no-cache');//增加 // $res->headers->set('pragma','max-age=5');//修改 // $res->headers->remove('pragma');//刪除 //跳轉(zhuǎn) //方法一 // $res->headers->add('location','http://www.baidu.com'); //方法二 // $this->redirect('http://www.baidu.com'); //文件下載 //方法一 // $res->headers->add('content-disposition','attachment;filename=a.jpg'); //方法二 // $res->sendFile('./robots.txt'); }
關(guān)于使用Yii 框架怎么創(chuàng)建一個(gè)控制器就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁(yè)標(biāo)題:使用Yii框架怎么創(chuàng)建一個(gè)控制器-創(chuàng)新互聯(lián)
URL分享:http://aaarwkj.com/article42/icdhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、App設(shè)計(jì)、外貿(mào)建站、企業(yè)建站、服務(wù)器托管、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容