Detailed Explanation of ThinkPHP Empty Module and Empty Operation

  • 2021-07-06 10:30:14
  • OfStack

The empty module and empty operation of ThinkPHP are also very practical functions. The concept of empty module is that when ThinkPHP can't find the specified module, it will try to locate the empty module (EmptyAction) and perform index operation in the empty module. Similarly, null operation is the same concept. When the system can't find the operation method under the specified module, it will try to locate the null operation method (empty). In fact, it is very easy to understand. It is a bit similar to the custom 404 page in 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 URL. The following details are introduced in detail about the writing of empty modules and empty operations.

1. Empty module, define EmptyAction class in project:


<?php
public class EmptyAction extends Action {
public function index(){
echo " Current module does not exist ";
  }
 }
?>

This is a simple empty module class. Of course, you can do some more complicated operations in it. All of them have to be written according to the needs of the project. Here is just a demonstration.

2. Null operation, which is defined under the specified module. For example, we define an empty operation under the module User, that is, the class UserAction.


<?php
class UserAction extends Action
{
public function index()
{
$this->display();
  }
public function demo(){$this->display();
  }
public function _empty(){
   // This method is an empty operation 
   echo ' Current operation does not exist ';
  }
 }
?>

The code is very simple, that is, an empty method, and empty modules and empty operations can also be used at the same time to complete more complex operations.


Related articles: