Home >Backend Development >PHP Tutorial >ThinkPHP controller modules and operations_PHP tutorial

ThinkPHP controller modules and operations_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:38:05897browse

ThinkPHP uses modules and operations to execute. First, the user's request will generate an application instance through the entry file. The application controller (we call it the core controller) will manage the entire user execution process. It is responsible for module scheduling and operation execution, and finally destroys the application instance. Any WEB behavior can be considered as an operation of a module. The system will analyze the module and operation to be performed based on the current URL. This analysis work is implemented by the URL scheduler, and the official built-in Dispatcher class completes the scheduling. In the Dispatcher scheduler, it will be based on

http://servername/appName/moduleName/actionName/params

to get the project (appName), module (moduleName) and operation (actionName) that currently need to be executed. In some cases, appName is not required (usually the homepage of the website, because the project name can be specified in the entry file , in this case, appName will be replaced by the entry file). In more complex situations, grouping (groupName) may also appear.

Each module is an Action file, similar to what we usually call a controller. The system will automatically look for related classes under the Action directory of the project class library. If not found, it will locate the empty module, otherwise an exception will be thrown. .

The actionName operation is to first determine whether there is a public method of the Action class. If it does not exist, it will continue to look for the method in the parent class. If it still does not exist, it will look for whether there is an automatically matching template file. If a template file exists, the template output will be rendered directly.

Therefore, an important process in application development is to define specific operations for different modules. If an application does not need to interact with the database, it does not need to define a model class, but it must define an Action controller. The definition of Action controller is very simple, just inherit the Action base class, for example:

Collapse and expand PHP Code copy content to clipboard
  1. class UserAction extends Action{
  2. }

If we want to execute the following URL

http://servername/index.php/User/add

You just need to add an add method, such as

Collapse and expand PHP Code copy content to clipboard
  1. class UserAction extends Action{
  2. // Define an add operation method. Note that the operation method does not require any parameters.
  3. Public function add(){
  4. // Logical implementation of add operation method
  5. // … 
  6. $this->display(); // Output template page
  7. }
  8. }

The operation method must be defined as Public type, otherwise an error will be reported. And be careful not to duplicate the naming of the operation method with the method 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 TpldefaultUseradd.html under the project directory.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735135.htmlTechArticleThinkPHP is executed in the form of modules and operations. First, the user's request will generate an application instance through the entry file. The application controller (we call it the core controller) manages...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn