First recognized ThinkPHP controller, first recognized thinkphp. ThinkPHP controller first, thinkphp controller definition and basic operations described in this article. I hope you can have a preliminary understanding of ThinkPHP controller. ThinkPHP controller and thinkphp controller
This article focuses on the definition and basic operations of the ThinkPHP controller. I hope you can have a preliminary understanding of the ThinkPHP controller.
The most basic controller:
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { public function index(){ } public function hello(){ echo 'hello'; }}
The Controller name is named in upper case by using the hump method. the Controller file is located in Application/Home/Controller/IndexController. class. php.
The hello method of the IndexController controller class is the operation method. visit the following URL address:
Http: // serverName/Home/Index/hello
The output is "hello"
Front and back operations:
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { public function _before_index(){ echo "index.before
"; } public function index(){ echo "index
"; } public function _after_index(){ echo "index.after
"; }}
Configure ACTION_SUFFIX to change the operation method writing mode:
Because the operation method is a controller method, you may not be able to define a method that conflicts with the system keyword. in this case, you can set the suffix of the operation method to solve the problem. for example
'Action _ SUFFIX '=> 'action', // operation method SUFFIX
Set the suffix of the operation method to Action. in this way, the operation method definition of the controller is adjusted:
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { public function listAction(){ echo 'list'; } public function helloAction(){ echo 'hello'; } public function testAction(){ echo 'test'; }}
Empty controller and empty operation method:
When the system cannot find the requested operation method, it will locate the empty operation (_ empty) method for execution, we can optimize error pages and URLs.
As shown in, when accessing:
Http: // serverName/index. php/Home/City/beijing/
Because the City controller does not define beijing, shanghai, or shenzhen operation methods, the system will locate the null operation method _ empty for parsing, the parameter of the _ empty method is the operation name in the current URL. Therefore, the output result is as follows:
How did you find me?
Binding an operation to a class: (function: you can define a class for each operation method, rather than a method of the controller class)
Take URL access as an example: http: // serverName/Home/Index/index,
The original Controller file definition location is: Application/Home/Controller/IndexController. class. php
The controller class is defined as follows:
Namespace Home \ Controller; use Think \ Controller; class IndexController extends Controller {public function index () {echo 'perform Index operations on the index Controller ';}}
We can see that we actually call the index method of the Home \ Controller \ IndexController class.
Set parameters through the configuration file
'Action _ BIND_CLASS '=> True,
After setting, change the Controller file location to Application/Home/Controller/Index/index. class. php.
The controller class is defined as follows:
Namespace Home \ Controller \ Index; use Think \ Controller; class index extends Controller {public function run () {echo 'perform Index operations on the index Controller ';}}
Now, we call the run method of the Home \ Controller \ Index \ index class.
The above is all the content of this article. I hope it will help you learn php programming.
Articles you may be interested in:
- Thinkphp controller scheduling example
- Description of ThinkPHP3.2.2 plug-in controller functions
- Relationship between URL path access and Module Controller in ThinkPHP
- Mutual call between ThinkPHP controllers
- Solution to the failure to execute javascript code in the ThinkPHP controller
- ThinkPHP3.2.2 plug-in controller function
- ThinkPHP Controller Details
The definition and basic operations of the ThinkPHP controller described in this article. I hope you can have a preliminary understanding of the ThinkPHP controller. Basic...