Home > Article > Backend Development > ThinkPHP empty modules and empty operations detailed explanation_PHP tutorial
ThinkPHP’s empty modules and empty operations are also very practical functions. The concept of empty modules is that when ThinkPHP cannot find the specified module, it will try to locate the empty module (EmptyAction) and execute the index in the empty module. operate. In the same way, empty operations have the same concept. When the system cannot find the operation method under the specified module, it will try to locate the empty operation method (empty). In fact, it is easy to understand. It is somewhat similar to the custom 404 page in the PHP virtual host, but it is more flexible than the custom 404. Using this mechanism, we can realize the optimization of error pages and some URLs. The empty modules and the following are introduced in detail. How to write no operation.
1. Empty module , define the EmptyAction class in the project:
<?php public class EmptyAction extends Action { public function index(){ echo "当前模块不存在"; } } ?>
This is a simple empty module class. Of course, you can also do some more complex operations in it. Everything must be written according to the needs of the project. This is just a demonstration.
2. No-op , no-op is defined under the specified module. For example, we define a no-op under the User module, which is the UserAction class.
<?php class UserAction extends Action { public function index() { $this->display(); } public function demo(){$this->display(); } public function _empty(){ //该方法即为空操作 echo '当前操作不存在'; } } ?>
The code is very simple, this is an empty method, and empty modules and empty operations can be used at the same time to complete more complex operations.