One, empty operation and NULL controller processing
Null operation: No specified action method; NULL controller: No controller specified, for example:
http =/url/index.php/home/main/login normal
http://url/index.php/home/main/Hello null operation (Hello method does not exist)
HTTP//url/index.php/home/Beijing/login NULL controller (Beijing controller does not exist)
1, empty operation:
General website Security considerations do not prompt users for any error messages
"empty operation" essentially means that an object ( controller ) calls itself a method that does not exist
in OOP , the object call itself does not exist methods, in the user experience is a good point of view, we can create a magic method inside the class:function __call ();
in the TP inside the controller's parent class:/tp/thinkphp/library/think/controller.class.php, which has a method:
So there are two solutions to an empty operation:
(1) in the corresponding controller to make a method, the name is "_empty", the controller's empty operation will automatically execute the method . (recommended) do not build in the parent class, but in the subclass .
function _empty () { echo ' ; }
This allows you to access the controller under an empty operation:
(2)   !--[endif]--> , for example: home/main/hello.html
< Body > < H1 > Don't Mess around! </ H1 > </ Body >
This allows you to access the empty operation:
However, this method is limited to this one empty operation , if access to other empty operations, or there will be error messages, due to the uncertainty of the null operation, so it is not recommended to use, know this method is OK.
2. Empty controller
NULL controller: The specified class was not found when instantiating the controller object.
Inside the thinkphp/library/think/app.class.php include the Controller object creation, and the object invokes the specified method to render the content:
NULL controller processing scheme: You can make another controller: home/controller/emptycontroller.class.php
Only one _empty () method can be created inside the controller:
<? phpnamespace Home\controller; Use Think\controller; class extends controller{ function _empty () { echo ' ; } }
This request again:
Second, cross-controller call method
For example: Call the Shuchu method in the index controller inside the main controller
Index Controller:
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction shuchu () { return ' Educational administration System "; }}
The controller is a class, to invoke the inside of the method, first to instantiate the object, and then through the object to invoke the method, here are 3 methods can be implemented:
- New way of making the object and then calling the Shuchu method
- Using the A method to quickly create objects, equivalent to new way, more convenient, recommended to use
- Using the R method, you can not only create objects, but also call methods
Main controller:
<?Phpnamespace Home\controller; UseThink\controller;classMaincontrollerextendsController {functionShuchu () {//Cross-controller Access operations (methods) because the controller is a class, you can instantiate the object to invoke the method//1 inside. Created Objects $index=NewIndexcontroller (); //Calling Methods Echo $index-Shuchu (); //2.//Use a method to quickly create objects $index= A ("Index"); Echo $index-Shuchu (); //3.//Use the R method to create an object and invoke a specific method EchoR ("Index/shuchu"); }}
This requests the Shuchu method in the main controller:
Three, cross-module call
Originally there is a home module, we follow the rules of the home module to create a new admin module, the folder inside the same as the home module: Under jiaowu/New Admin folder, under admin/new five folders: Controller/,model /,view/,common/,conf/. Create a new TestController.class.php under controller/:
<? Admin Use Think\controller; class extends controller{ publicfunction Test () { return ' Admin module Operation "; } }
In the Home/main/shuchu down with the Test method under admin/test/, simply precede the creation of the object with the module name:
<?Phpnamespace Home\controller; UseThink\controller;classMaincontrollerextendsController {functionShuchu () {//calling methods across modules//using a method $test= A ("Admin/test"); Echo $test-Test (); //using the R method EchoR ("Admin/test/test"); }}
This accesses the Shuchu method:
Iv. definition and use of namespaces
We normally manage files using folders, which are physically differentiated;
The namespace is equivalent to a virtual directory, although not under the same folder, but still belongs to the same class under the virtual directory
The initial namespace for the TP framework is:thinkphp/library
The namespace inside the TP framework uses \ for the initial namespace (thinkphp\library)
1. The root namespace under the system directory is the folder name under Thinkphp/library , such as: think,org
2. The module's root namespace is named after the module name , such as: Home,admin
when defining a namespace, write from the root , for example: namespace Home\controller; namespace Think\model;
If you want to use a class file, introduce the class namespace by using the use keyword, followed by the namespace of the class , followed by the "\ File name ", for example: usage think\ Controller is the introduction of think under the Controller.class.php class file.
If you do not want to introduce namespaces, you can use \(the initial namespace) to find the corresponding class when you create the object of the class, for example: New \think\page (30)
Note: namespaces use a backslash \
thinkphp Framework Basic Knowledge II