CakePHP2Request request object Chinese tutorial

Source: Internet
Author: User
CakeRequest is the default request object of CakePHP. This class is used to process and interact with request data. In each request process, the CakeRequest object is created and passed to the layer where the application needs to use the data (such as a controller or view) through reference ). By default, the CakeRequest object is assigned $ this-& gt; request, which can be in the controller, view, and helper class.


CakeRequest is the default request object of CakePHP. This class is used to process and interact with request data. In each request process, the CakeRequest object is created and passed to the layer where the application needs to use the data (such as a controller or view) through reference ). By default, the CakeRequest object is assigned $ this-> request, which can be used in controllers, views, and helper classes. You can also use the request object in the component class by referencing the controller. In general, the CakeRequest object is mainly responsible for the following functions:

  • Process the GET, POST, and FILES arrays and return the data as objects.
  • Provides information about the client that initiates the request, such as headers, client IP address, and domain name.
  • Provides methods to obtain request parameters, including arrays and object attributes.
GET request parameters

CakeRequest provides multiple interfaces for obtaining request parameters. The first method is the array index, the second is the $ this-> request-params, and the third is the object attribute. For example, obtain the controller of the current request.

010203 $ This-> request ['controller']; $ this-> request-> controller; $ this-> request-> params ['controller']

The controller name of the current request can be obtained in the preceding three methods. Request parameters can be obtained in multiple ways to facilitate application migration and upgrade. A. Not only can the controller be obtained through the above method, but all routing elements can be obtained through these interfaces.

In addition to routing elements, B, the most commonly used request parameters include the common parameters (Passed arguments) and naming parameters () in the URL (). These parameters can also be obtained through three interfaces of the request object.

010203040506070809 // Passed arguments $ this-> request ['pass'] $ this-> request-> pass $ this-> request-> params ['pass'] // named parameters $ this-> request ['named'] $ this-> request-> named $ this-> request-> params ['named']

Common parameters and named parameters are also obtained through the three CakeRequest interfaces. C. Note that CakePHP has many important and useful parameters that can be obtained through the request parameters of the CakeRequest object.

  • The plug-in that the plug-in processes the current request. If no plug-in is available, null is returned.
  • The controller that processes the current request
  • Action: Controller method for processing the current request
  • Prefix the prefix of the current controller. For example, admin_edit. Configure in route.
  • The parameters contained in the request initiated by bare through requestAction.
  • The value of requested is true when the request comes from requestAction.
Obtain the query string parameter Accessing Querystring parameters

This is the most common URL form in a PHP program that has not been rewritten. you can use CakeRequest: $ query to obtain the query string parameter.

010203 // URL/posts/index? Page = 1 & sort = title $ this-> request-> query ['Page'] $ this-> request ['URL'] ['Page']
Get POST data Accessing POST data

All data transmitted in the form of POST can be obtained through CakeRequest: $ data. All the data in the form that contains the data prefix can be obtained by removing the data string and CakeRequest: $ data. For example.

0102 // When the name attribute of a form field is data [Post] [title, you can obtain this value in the submitted controller by using the following method: $ this-request-> data ['post'] ['title'];
Get path information Accessing path information

CakeRequest provides the path information about the current application. CakeRequest: $ base and CakeRequest: $ webroot are used to generate url addresses. In addition, they can automatically determine whether the current program is in a subdirectory. (I have never used these two methods to determine sub-directories. I will add more detailed usage methods here later ).

Check request Inspecting the request

Check different request environments. before version 2.0, it is generally established through RequestHandlerComponent. The new version of CakePHP has unified these methods into the CakeRequest request object, and provides a new interface for backward compatibility.

0102 $ This-> request-> is ('post'); $ this-> request-> isPost ();

Both methods can detect whether the current request is initiated through POST. Here there are extensions used to create a new method to detect the request environment, as shown below.

01020304050607080910111213141516 // Add an environment detector. $ this-> request-> addDetector ('post', array ('env' => 'request _ method', 'value' => 'post ')); // Add a pattern value detector. $ this-> request-> addDetector ('iPhone ', array ('env' => 'http _ USER_AGENT', 'pattern' => '/iphone/I ')); // Add an option detector $ this-> request-> addDetector ('internalip', array ('env' => 'Client _ IP ', 'options' => array ('123. 168.0.101 ', '2017. 168.0.100 '); // Add a callback detector. can either be an anonymous function or a regular callable. $ this-> request-> addDetector ('awes', array ('callback' => function ($ request) {returnisset ($ request-> awesome );}));

The default environment detection method of the framework is as follows,

  • Is ('get') Check to see if the current request is a get.
  • Is ('put') Check to see if the current request is a put.
  • Is ('post') Check to see if the current request is a post.
  • Is ('delete') Check to see if the current request is a delete.
  • Is ('head') Check to see if the current request is head.
  • Is ('options') Check to see if the current request is options.
  • Is ('Ajax ') Check to see of the current request came with X-Requested-with = XmlHttpRequest.
  • Is ('SSL ') Check to see if the request is via ssl
  • Is ('Flash') Check to see if the request has a User-Agent of flash
  • Is ('mobile') Check to see if the request came from a common list of mobile agents.
Interacting with other aspects of the request

Here we will talk again about the methods and attributes of the path and current URL address in CakeRequest for comparison.

  • $ This-> request-> webroot contains the path of the current root directory
  • $ This-> request-> base is equivalent to the base path obtained in the PHP function.
  • $ This-> request-> here get the complete path of the current request
  • $ This-> request-> query contains query string parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.