Thinkphp 3.1 QuickStart: Controller advanced Features

Source: Internet
Author: User
Keywords QuickStart thinkphp advanced features
Tags .url access action class address advanced advanced features aliyun app

The thinkphp controller layer consists of a core controller and a business controller, and the core controller is completed by the app class within the system, responsible for the application (including the module and operation) scheduling control, including HTTP request interception and forwarding, loading configuration, the business controller is user-defined http:// Www.aliyun.com/zixun/aggregation/16339.html ">action class or other controller class complete.
We have learned from the previous study, the basic controller usage, this article we describe some of the controller's characteristics and advanced usage, to explore the thinkphp controller's mystery cloak.

Action parameter Binding

In the previous section, all of the action methods we were involved in did not have any parameters, actually starting with version 3.1, you can support the parameter binding function. The principle of the action parameter binding is to bind the parameters in the URL (excluding grouping, module, and operation name) and the parameters in the action method of the controller (by variable name).
For example, we have defined two action methods read and archive methods for the blog module, and you need to specify an ID parameter for the read operation, the archive method specifies the year and the month (month) two parameters. For demonstration convenience, we omit the business code for the specific operation method, and simply output the current parameter with Echo. The URL's access address is: The ID parameter in the two URL address and the year and month parameters are automatically bound to the read action method and the parameter with the same name as the archive action method.
The result of the output is that the parameter of the action parameter must match the name of the parameter passed in the URL, but the order of the parameters does not need to be consistent. In other words, the results are consistent with the above access, the parameters in the URL and the order of the parameters in the operation method can be arbitrarily adjusted, the key is to ensure that the parameter name consistent.
If the user accesses the URL address (as to why the visit is not mentioned): it throws the following exception hint: The reason for the error is simple, because the ID parameter must pass in the parameter when the Read action method is executed, but the method cannot get the correct ID parameter information from the URL address. Since we cannot trust any input from the user, therefore, it is recommended that you add a default value to the ID parameter of the Read method, for example: this way, when we access it, we output the output: The parameter binding function is not affected by the route, and is as valid as the parameters passed in the URL from the route. And the binding parameters require special handling and filtering if necessary.

Class Blogaction extends action{public function read ($id) {echo ' id= '. $id; The Public function archive ($year = ', $month = ') {echo ' year= '. $year. '     &month= '. $month; } HTTP://SERVERNAME/INDEX.PHP/BLOG/READ/ID/5 http://serverName/index.php/Blog/archive/year/2012/month/03 id=5 year=2012&month=03 http://serverName/index.php/Blog/archive/month/03/year/2012 http://serverName/index.php/ blog/read/parameter error: ID public function Read ($id =0) {echo ' id= '. $id;} http://serverName/index.php/Blog/read/id=0 http:// Servername/index.php/blog/archive/year=2012&month=01

Related Article

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.