This article focuses on the definition of thinkphp controller, the basic operating content, I hope you can have a preliminary understanding of thinkphp Controller.
The most basic controller:
<?php
namespace Home\controller;
Use Think\controller;
Class Indexcontroller extends Controller {public
function index () {
} public
function hello () {
echo ' Hello ';
}
}
The name of the controller is named after the Hump (first letter), and the controller file is located in application/home/controller/indexcontroller.class.php
The Hello method for the Indexcontroller controller class is the action method that accesses the following URL address:
Http://serverName/Home/Index/hello
It will output "hello."
Predecessors and post operations:
<?php
namespace Home\controller;
Use Think\controller;
Class Indexcontroller extends Controller {public
function _before_index () {
echo "index.before<br>";
} Public
Function Index () {
echo "index<br>";
}
Public Function _after_index () {
echo ' index.after<br> ';
}
}
Configure Action_suffix to change the way you write:
Because the method of operation is a method of the controller, so the method of encountering the keyword conflict with the system may not be defined, this time we can set the suffix of the action method to solve, for example
' Action_suffix ' => ' action ',//action method suffix
Sets the suffix of the action method to action so that the Controller's action method definition is adjusted to:
<?php
namespace Home\controller;
Use Think\controller;
Class Indexcontroller extends Controller {public
function listaction () {
echo ' list ';
}
Public Function helloaction () {
echo ' hello ';
}
Public Function testaction () {
echo ' Test ';
}
}
NULL controller and NULL operation method:
Null operation refers to the system can not find the requested operation method, will be positioned to the empty operation (_empty) method to execute, using this mechanism, we could implement the error page and some URL optimization.
As shown in the figure above, when accessing:
http://serverName/index.php/Home/City/beijing/
Because the city controller does not define the Beijing, Shanghai or Shenzhen operation methods, the system will be positioned into the null operation method _empty to resolve, the _empty method parameter is the current URL inside the operation name, so you will see the output in turn is:
How did you find me?
Actions are bound to classes: (function: A method that defines a class for each action method, not a controller class)
Take URL access as an example of 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 ' performs the index operation of the index controller ';
}
}
As you can see, we're actually calling the index method of the Home\controller\indexcontroller class.
Setting parameters by configuration file
' Action_bind_class ' => True,
When set, the controller file location is changed 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 ' performs the index operation of the index controller ';
}
}
Now, we're actually calling the run method of the Home\controller\index\index class.
The above is the entire content of this article, I hope that you learn PHP programming help.