Generally, the Controller of ThinkPHP is a class, while the operation is a public method of the controller class. Next, let's talk about the ThinkPHP controller in detail.
Generally, the Controller of ThinkPHP is a class, while the operation is a public method of the controller class. Next, let's talk about the ThinkPHP controller in detail.
In the last course, you may have some questions about ThinkPHP routing, but it doesn't matter. After learning this course, many things will suddenly become open.
The Controller file name follows the IndexController. class. php method.
Controller Definition
Before starting, we still need to clarify the definition of the controller:
<? Phpnamespace Home \ Controller; use Think \ Controller; class IndexController extends Controller {public function read ($ id) {echo "read page
". $ Id;} public function top () {echo" top page
";}}
As you can see, the Controller mentioned in the routing section is defined as follows:
Use the corresponding namespace. The default namespace is Home \ Controller.
Load Think \ Controller
The new Controller inherits from the Controller (or subclass)
The Hump naming method is used. Note that the first letter is capitalized.
The public methods in the controller can be regarded as an operation. For example, the read () and top () methods above can be regarded as operations. We have verified them in the routing article.
: 8999/index. php/Home/Index/top
Is to access the top () method, the top page will be printed on the page, and again it is clear that Home represents the Home Module
Sometimes there may be conflicting methods with system keywords. In this case, you can use the suffix of the operation method to solve the problem. For details, see the official documentation:
Front and back operations
Pre-and post-operations refer to methods that are automatically called before and after an operation method is executed. However, they are only valid for the access controller, for example, top () in IndexController () method to add the pre-post method:
Public function _ before_top () {echo "before top page
";} Public function top () {echo" top page
";} Public function _ after_top () {echo" after top page
";}
Access: 8999/index. php/Home/Index/top
The output is as follows:
Before top pagetop pageafter top page
Note the following before and after operations:
If the current operation does not define the operation method, but directly renders the template file, it will still take effect if the pre-and post-methods are defined. The real template output may only be the current operation. The front and back operations generally do not have any output.
Note that if exit or error output is used in some methods, the POST method may not be executed. For example, if the system Action error method is called in the current operation, the post operation will not be executed, but the execution of the POST method of the success method will not be affected.
It can be used for form filtering and verification.
Parameter binding
Parameter binding directly binds the variables in the URL address as the parameters of the operation method, which can simplify the definition of the method and even parse the route.
'Url _ params_bind' => true
The parameter binding function is enabled by default. The principle is to bind parameters (excluding modules, controllers, and operation names) in the URL to parameters in the operation method.
There are two ways to bind parameters: bind according to the variable name and bind according to the variable order. By default, bind according to the variable name. For example, see the following example:
Public function read ($ id) {echo "read page
". $ Id;} public function archive ($ year, $ month) {echo" $ year
". $ Month ;}
Yes, this is the content of the previous route.
'Blogs/: id' => array ('index/read ')
We map the id directly to the $ id parameter of the read () method, so now let's look back. In fact, the routing rule gives you a custom URL function. If the preceding route settings are removed, the correct access method is:
: 8999/Home/index/read/id/3
In the above URl, id is the variable name. If you write it:
Public function read ($ title) {echo "read page
". $ Title ;}
The access address is:
: 8999/index. php/Home/index/read/title/3
When multiple parameters are bound, only the corresponding variable names and values can be passed in, regardless of the order. For example, the following two will return the same result:
: 8999/index. php/Home/index/archive/year/2012/month/12
: 8999/index. php/Home/index/archive/month/12/year/2012
It should be noted that, in any case, when you access
: 8999/index. php/Home/index/read/
Yes, an error is reported:
Parameter error or undefined: id
A good solution is to set the default value for the bound parameter, for example:
Public function read ($ id = 0) {echo "read page
". $ Id ;}
In this way, the above URL will be accessed again and the output will be:
Read page
0
Tips: setting default values for binding parameters is a good way to avoid errors.
In actual development, we will see no URLs with variable names, such:
: 8999/index. php/Home/index/read/3
How can this problem be solved? In this case, we can actually use the second parameter binding: Variable binding. To bind this parameter, you must first set it in the settings:
'Url _ PARAMS_BIND_TYPE '=> 1
Once variable order binding is set, the parameter order in the URL address is very important and cannot be adjusted at will. In this case, the definition of the operation method does not need to be changed, but the access URL has changed. Now, you can access the method correctly.
If the variable order is bound, we access:
: 8999/index. php/Home/index/archive/2012/12
: 8999/index. php/Home/index/archive/12/2012
The two results are obviously different. The latter is not what we want. Therefore, values must be transmitted in strict order.
Pseudo-static
The URL pseudo-static is usually used to achieve better SEO performance. ThinkPHP supports pseudo-static URL settings. You can add the desired static suffix at the end of the URL by setting the URL_HTML_SUFFIX parameter ,, the normal execution of the current operation is not affected. By default, the pseudo-static settings are html. But we can set it by ourselves, for example
'Url _ HTML_SUFFIX '=> 'shtml'
If you want to support multiple pseudo-static suffixes, you can directly set them as follows:
'Url _ HTML_SUFFIX '=> 'html | shtml | xml'
If this setting is left blank, all static suffixes are supported.
You can also set the URL Suffix of the forbidden access through URL_DENY_SUFFIX. For example:
'Url _ DENY_SUFFIX '=> 'pdf | ico | png | gif | jpg ',
Note: URL_DENY_SUFFIX has a higher priority than URL_HTML_SUFFIX.
URL generation
To work with the URL mode, we need to be able to dynamically generate the corresponding URL address based on the current URL settings. For this reason, ThinkPHP provides a built-in U Method for Dynamic URL generation, this ensures that the project is not affected by the environment during the migration process.
Define rules
The U method is defined as follows (parameters in square brackets are determined based on actual application ):
U ('address expression', ['parameter'], ['pseudo-static suffixes '], ['display domain name'])
Address expression
The format of an address expression is defined as follows:
[Module/controller/Operation # anchor @ domain name]? Parameter 1 = value 1 & Parameter 2 = value 2...
If no module is defined, it indicates the current Module name. Below are some simple examples: