Yii 請求與響應

一:請求 web

在Yii中一個應用的請求是用 yiiwebRequest 對象來表示的ajax

1:請求參數服務器

(1):get傳參併發

$request = \Yii::$app->request;  
//獲取get傳參的全部參數  
$get = $request->get();  
//獲取get傳參的指定參數  
$id = $request->get('id');  
//獲取get傳參的指定參數,無傳參設置默認值  
$id = $request->get('id', 1);

(2):post傳參app

$request = \Yii::$app->request;  
//獲取post傳參的全部參數  
$post = $request->post();  
//獲取post傳參的指定參數  
$id = $request->post('id');  
//獲取post傳參的指定參數,無傳參設置默認值  
$id = $request->post('id', 1);

(3):獲取全部傳參方式的參數 yii

咱們常常須要獲取經過 PUT, PATCH 或者其餘的請求方法提交上來的參數。這時候你能夠經過調用 yiiwebRequest::getBodyParam()方法來獲取這些參數post

$request = \Yii::$app->request;  
//獲取請求的全部參數  
$params = $request->bodyParams;  
//獲取請求的指定參數  
$param = $request->getBodyParam('id');  
//獲取請求的指定參數,無傳參設置默認值  
$param = $request->getBodyParam('id', 1);

2:請求方法 url

在Yii中咱們能夠經過yiiwebRequest 對象知道當前的請求方式是什麼code

$request = \Yii::$app->request;  
//獲取當前請求方式  
$method = $request->method;  
//判斷當前請求是不是ajax請求方式  
$isAjax = $request->isAjax;  
//判斷當前請求是不是get請求方式  
$isGet = $request->isGet;  
//判斷當前請求是不是post請求方式  
$isPost = $request->isPost;  
//判斷當前請求是不是put請求方式  
$isPut = $request->isPut;

3:請求Urlorm

在Yii中咱們能夠經過yiiwebRequest 對象獲取Url相關信息

$request = \Yii::$app->request;  
//獲取當前路由不包括主機信息部分的路由  
$url = $request->url;  
//獲取當前路由包括主機信息部分的路由  
$absoluteUrl = $request->absoluteUrl;  
//獲取當前路由的主機信息部分  
$hostInfo = $request->hostInfo;  
//獲取入口腳本後到問號以前的信息  
$pathInfo = $request->pathInfo;  
//獲取問號以後的信息  
$queryString = $request->queryString;  
//獲取主機信息以後入口腳本以前的信息  
$baseUrl = $request->baseUrl;  
//獲取沒有路徑信息和查詢字符串部分  
$scriptUrl = $request->scriptUrl;  
//獲取Url中主機名  
$serverName = $request->serverName;  
//獲取使用的端口號  
$serverPort = $request->serverPort;

4:請求header頭信息

在Yii中咱們能夠經過yiiwebRequest::$headers屬性來獲取header頭信息的相關信息

$request = \Yii::$app->request;  
//返回請求的header頭信息對象  
$headers = $request->headers;  
//獲取指定參數的header頭信息  
$accept = $headers->get('Accept');  
//判斷致電給參數的header頭信息是否存在  
$hasAccept = $headers->has('Accept');

5::獲取客戶端相關信息

在Yii中咱們還能夠經過yiiwebRequest對象 來獲取客戶端的相關信息

$request = \Yii::$app->request;  
//獲取客戶端主機名  
$userHost = $request->userHost;  
//獲取客戶端ip地址  
$userIP = $request->userIP;

二:響應

當一個應用處理完一個請求以後,這時候應用須要發送一個響應給終端用戶,在yii中使用yiiwebResponse對象來處理這些響應

1:狀態碼響應

$response = \Yii::$app->response;  
//設置響應狀態碼  
$response->statusCode = 200;

 yiiwebResponse::$statusCode屬性的默認值爲200, 若是須要指定請求失敗,可拋出對應的 HTTP 異常,如:

throw new NotFoundHttpException('拋出404錯誤');  
throw new BadRequestHttpException('拋出400錯誤');  
throw new ConflictHttpException('拋出409錯誤');  
throw new ForbiddenHttpException('拋出403錯誤');  
throw new GoneHttpException('拋出410錯誤');  
throw new MethodNotAllowedHttpException('拋出405錯誤');  
throw new NotAcceptableHttpException('拋出406錯誤');  
throw new NotFoundHttpException('拋出404錯誤');  
throw new ServerErrorHttpException('拋出500錯誤');  
throw new TooManyRequestsHttpException('拋出429錯誤');  
throw new UnauthorizedHttpException('拋出401錯誤');  
throw new UnsupportedMediaTypeHttpException('拋出415錯誤');

若是想拋出的異常不在如上列表中,可建立一個 yiiwebHttpException 異常, 帶上狀態碼拋出

throw new HttpException('402', '拋出402錯誤');

2:響應http的header頭信息

$response = \Yii::$app->response;  
//返回響應的header頭信息對象  
$headers = $response->headers;  
//增長一個指定的header頭信息,該header頭信息存在不會覆蓋  
$addPragma = $headers->add('Pragma', 'no-cache');  
//設置一個指定的header頭信息,該header頭信息存在會覆蓋  
$setPragma = $headers->set('Pragma', 'no-cache');  
//刪除一個指定的header頭信息  
$deletePragma = $headers->remove('Pragma');

3:響應內容

大多數響應應有一個主體存放你想要顯示給終端用戶的內容

(1):若是你有一個已經格式化好的響應主題,你一個將其賦值到 yiiwebResponse::$content 屬性

$response = \Yii::$app->response;  
$response->content = 'test';

(2):若是你發送到終端的數據須要進行格式化的話,yiiwebResponse::$format和yii\web\Response::$data屬性

$response = \Yii::$app->response;  
//設置響應主體的格式  
$response->format = \yii\web\Response::FORMAT_JSON;  
//響應應用主體  
$response->data = ['message' => 'test'];

(3):除了使用默認的 response 應用組件,也可建立本身的響應對象併發送給終端用戶, 可在操做方法中返回該響應對象

return \Yii::createObject([
    'class' => Response::className(),
    'format' => \yii\web\Response::FORMAT_JSON,
    'data' => [
        'message' => 'test'
    ]
]);

4:發送文件

在Yii中yiiwebResponse對象中能夠實現發送文件,即將服務器中的指定文件下載到本地,實現下載功能

發送文件主要用到了下面三種方式

yiiwebResponse::sendFile():發送一個已存在的文件到客戶端

yiiwebResponse::sendContentAsFile():發送一個文本字符串做爲文件到客戶端

yiiwebResponse::sendStreamAsFile():發送一個已存在的文件流做爲文件到客戶端

如:

(1):yiiwebResponse::sendFile()

將指定地址的指定文件從服務器上下載到本地

$response = \Yii::$app->response;  
$response->sendFile('test.txt')->send();

(2):yii\webResponse::sendContentAsFile()

將指定字符下載到本地

$response = \Yii::$app->response;  
$response->sendContentAsFile('test','test.txt')->send();

(3):yiiwebResponse::sendStreamAsFile()

將指定文件流從服務器下載到本地

$response = \Yii::$app->response;  
$handle = fopen('test.txt', 'rb');  
$response->sendStreamAsFile($handle, 'test.txt')->send();
相關文章
相關標籤/搜索