ThinkPHP USES modules and operational analysis

  • 2020-03-31 21:36:52
  • OfStack

Any WEB behavior can be considered an operation of a module, and the system will analyze the module and operation to be performed based on the current URL. This analysis is done by the URL scheduler, and the Dispatcher class is officially built in to do this. In the Dispatcher Dispatcher, it will be based on
http://servername/appName/moduleName/actionName/params
To get the currently executed project (appName), module (moduleName), and action (actionName). In some cases, the appName may not be required (usually the front page of the website, since the project name can be specified in the entry file, in which case the appName is replaced by the entry file). In more complex cases, a groupName may also occur.
Each module is an Action file, similar to what we usually say about the controller. The system will automatically look for the relevant classes under the Action directory of the project class library. If not, it will locate the empty module, or throw an exception.
The actionName operation is the public method that first determines whether there is an Action class. If there is no actionName operation, it will continue to look for the method in the parent class. If there is still no actionName operation, it will look for whether there is an auto-matching template file. If a template file exists, render the template output directly.
Therefore, an important process in application development is to define specific operations for different modules. An application that does not need to interact with the database may not need to define a model class, but it must define an Action controller. The definition of the Action controller is very simple, as long as it inherits the Action base class, for example: microfiber cloth
 
class UserAction extends Action{ 
} 

If we want to execute the following URL
http://servername/index.php/User/add
You need to add an add method, for example
Collapse the PHP Code to copy the contents to the clipboard
 
class UserAction extends Action{ 
//Define an add action method, noting that the action method does not require any parameters
Public function add(){ 
//Logical implementation of the add action method
//  ...  bath rug 
$this->display(); //Output template page
} 
} 

The action method must be defined as Public or an error will be reported. Also be careful not to duplicate the names of Action methods with the methods of the built-in Action class. The system will automatically locate the template file for the current operation, and the default template file should be located in the Tpl\default\User\add.html under the project directory.

Related articles: