A Controller operation
Let's begin by reviewing the basic controller definition and method execution methods.
The first step: The controller is created by default under the Controller directory under the current module;
The second step: according to the specified naming rules: Controller name (capital letter) +controller.class.php;
The third step: The method in the controller must be public;
1 // Controller 2 class extends Controller {3public Function Index () {4/// the index () method can be ignored when the URL is accessed 5 }6}
URL Access: http://localhost/demo39/user/index/
Sometimes the method name may conflict with the keyword, then it is more difficult to define.
In Weibo/home/controller/indexcontroller. In class. PHP , if the method name coincides with a keyword in thinkphp, an error occurs, similar to the following:
1<?PHP2 namespace Home\controller;3 UseThink\controller;4 classIndexcontrollerextendsController {5 Public function class() {6 Echo"This is class";7 }8}
The error is as follows:
The controller provides a configuration definition:
To configure in weibo/conf/config.php :
1 // Configuring the Controller method suffix 2 ' action_suffix ' = ' ACTION ',
Add action after each method, no action required for URL access
1 classIndexcontrollerextendsController {2 Public functionindexaction () {3 Echo' Index ';4 }5 Public functionclassaction () {6 Echo' Test ';7 }8}
This is to enter HTTP in the browser ://localhost/demo39/index/class can be accessed,
Note that this is the address in the browser or the class, not the Classaction
By default, pages accessed through URLs are the methods under the Controller module, namely:
UserController.class.php class. It is publicly accessible and we can call it access controller. That
Many times, because of the high complexity of the project business, we may think of many businesses separating into another layer, such as events
Controller layer. Then you can do this:
First step: Create the Event Controller directory: event; (at this time event and controller sibling)
Step Two: Create the UserEvent.class.php class;
The code is as follows:
1 <? PHP 2 namespace Home\event; 3 4 class userevent {5 Public function Test () {6 Echo ' This is the event controller layer encapsulation, not public, belongs to the internal call! '; 7 }8 }
In Weibo/home/controller/indexcontroller. the call in class. PHP is as follows:
<? phpnamespace Home\controller; Use Think\controller; Use home\event\userevent; class extends Controller { public function event () { $userEvent = new userevent (); $userEvent, Test (); Echo ' Test ';} }
You can call success at this point:
PS: This call to other controllers can also use the shortcut call Method A ().
1 $userEvent = A (' User ', ' Event '); 2 $userEvent // user Invoke event under admin
The code is as follows:
1<?PHP2 3 namespace Home\controller;4 UseThink\controller;5 Usehome\event\userevent;6 7 classIndexcontrollerextendsController {8 Public functionevent () {9 //$userEvent = new Userevent ();Ten $userEvent= A (' User ', ' Event '); One $userEvent-test (); A Echo"Test"; - } -}
Controller [1]