介绍 入门 应用结构 请求处理 关键概念 配合数据库工作 接收用户数据 显示数据 安全 缓存 RESTfulWeb服务 开发工具 测试 高级专题 小部件 助手类 其他

发布于 2015-08-01 11:18:24 | 432 次阅读 | 评论: 0 | 来源: 网络整理

Requests made to an application are represented in terms of yiiwebRequest objects which provide information such as request parameters, HTTP headers, cookies, etc. For a given request, you can get access to the corresponding request object via the request application component which is an instance of yiiwebRequest, by default. In this section, we will describe how you can make use of this component in your applications.

Request Parameters

To get request parameters, you can call yiiwebRequest::get() and yiiwebRequest::post() methods of the request component. They return the values of $_GET and $_POST, respectively. For example,

$request = Yii::$app->request;

$get = $request->get(); 
// equivalent to: $get = $_GET;

$id = $request->get('id');   
// equivalent to: $id = isset($_GET['id']) ? $_GET['id'] : null;

$id = $request->get('id', 1);   
// equivalent to: $id = isset($_GET['id']) ? $_GET['id'] : 1;

$post = $request->post(); 
// equivalent to: $post = $_POST;

$name = $request->post('name');   
// equivalent to: $name = isset($_POST['name']) ? $_POST['name'] : null;

$name = $request->post('name', '');   
// equivalent to: $name = isset($_POST['name']) ? $_POST['name'] : '';

Info: Instead of directly accessing $_GET and $_POST to retrieve the request parameters, it is recommended that you get them via the request component as shown above. This will make writing tests easier because you can create a mock request component with faked request data.

When implementing RESTful APIs, you often need to retrieve parameters that are submitted via PUT, PATCH or other request methods. You can get these parameters by calling the yiiwebRequest::getBodyParam() methods. For example,

$request = Yii::$app->request;

// returns all parameters
$params = $request->bodyParams;

// returns the parameter "id"
$param = $request->getBodyParam('id');

Info: Unlike GET parameters, parameters submitted via POST, PUT, PATCH etc. are sent in the request body. The request component will parse these parameters when you access them through the methods described above. You can customize the way how these parameters are parsed by configuring the yiiwebRequest::parsers property.

Request Methods

You can get the HTTP method used by the current request via the expression Yii::$app->request->method. A whole set of boolean properties are also provided for you to check if the current method is of certain type. For example,

$request = Yii::$app->request;

if ($request->isAjax) { // the request is an AJAX request }
if ($request->isGet)  { // the request method is GET }
if ($request->isPost) { // the request method is POST }
if ($request->isPut)  { // the request method is PUT }

Request URLs

The request component provides many ways of inspecting the currently requested URL.

Assuming the URL being requested is http://example.com/admin/index.php/product?id=100, you can get various parts of this URL as summarized in the following:

  • yiiwebRequest::url: returns /admin/index.php/product?id=100, which is the URL without the host info part.
  • yiiwebRequest::absoluteUrl: returns http://example.com/admin/index.php/product?id=100, which is the whole URL including the host info part.
  • yiiwebRequest::hostInfo: returns http://example.com, which is the host info part of the URL.
  • yiiwebRequest::pathInfo: returns /product, which is the part after the entry script and before the question mark (query string).
  • yiiwebRequest::queryString: returns id=100, which is the part after the question mark.
  • yiiwebRequest::baseUrl: returns /admin, which is the part after the host info and before the entry script name.
  • yiiwebRequest::scriptUrl: returns /admin/index.php, which is the URL without path info and query string.
  • yiiwebRequest::serverName: returns example.com, which is the host name in the URL.
  • yiiwebRequest::serverPort: returns 80, which is the port used by the Web server.

HTTP Headers

You can get the HTTP header information through the yiiwebHeaderCollection returned by the yiiwebRequest::headers property. For example,

// $headers is an object of yiiwebHeaderCollection 
$headers = Yii::$app->request->headers;

// returns the Accept header value
$accept = $headers->get('Accept');

if ($headers->has('User-Agent')) { // there is User-Agent header }

The request component also provides support for quickly accessing some commonly used headers, including:

  • yiiwebRequest::userAgent: returns the value of the User-Agent header.
  • yiiwebRequest::contentType: returns the value of the Content-Type header which indicates the MIME type of the data in the request body.
  • yiiwebRequest::acceptableContentTypes: returns the content MIME types acceptable by users. The returned types are ordered by their quality score. Types with the highest scores will be returned first.
  • yiiwebRequest::acceptableLanguages: returns the languages acceptable by users. The returned languages are ordered by their preference level. The first element represents the most preferred language.

If your application supports multiple languages and you want to display pages in the language that is the most preferred by the end user, you may use the language negotiation method yiiwebRequest::getPreferredLanguage(). This method takes a list of languages supported by your application, compares them with yiiwebRequest::acceptableLanguages, and returns the most appropriate language.

Tip: You may also use the yiifiltersContentNegotiator filter to dynamically determine what content type and language should be used in the response. The filter implements the content negotiation on top of the properties and methods described above.

Client Information

You can get the host name and IP address of the client machine through yiiwebRequest::userHost and yiiwebRequest::userIP, respectively. For example,

$userHost = Yii::$app->request->userHost;
$userIP = Yii::$app->request->userIP;
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务