Overview of ThinkPHP implementation of cross module call operation method

  • 2021-07-01 06:59:32
  • OfStack

Using $this, you can call methods within the current module, but in many cases you will often call methods of other modules within the current module. ThinkPHP has built-in A method and R method, two special uppercase methods to handle cross-module invocation.

At present, there are index operation in Index module, User module, showName operation, User module and showName operation. Specific codes are as follows:


<?php
class UserAction extends Action{
 public function showName(){
 echo "Hello World";
 }
}
?>

We will call the above showName operation in the index operation of the Index module.

Actions are invoked across modules through the A method

The A method is used to instantiate other modules (when the new keyword is used). Once the module is instantiated, operations within the module can be invoked as objects.
Invoke the showName operation instance of the User module in the index operation of the Index module:


<?php
class IndexAction extends Action{
 public function index(){
 header("Content-Type:text/html; charset=utf-8");
 //  Instantiation  User  Module 
 $User = A('User');
 //  Call  User  Methods in modules 
 $User->showName();
 }
}
?>

The A method also supports cross-group invocation and cross-project invocation modules with the following syntax:

A(' < Project name:// > < Group name/ > Module name ')


//  Common examples are as follows 
A('User') // Call the current project's  User  Module, as in the example above 
A('Admin://User') // Call  Admin  Project's  User  Module 
A('Admin/User') // Call  Admin  Grouped  User  Module 
A('Admin://Tool/User') // Call  Admin  Project  Tool  Grouped  User  Module 

Actions are invoked across modules through the R method
ThinkPHP also provides the R method, which can directly call the operation methods of other modules, and change the above example using A method to R method:


<?php
class IndexAction extends Action{
 public function index(){
 header("Content-Type:text/html; charset=utf-8");
 //  Call  User  Methods in modules 
 R('User/showName');
 }
}
?>

The R method also supports cross-group invocation and cross-project invocation with the following syntax:

R(' < Project name:// > < Group name/ > Module name/action ' < , array() > )


//  Common examples are as follows 
R('User/showName') // Call the current project's  User  Modular  showName  Method, as in the example above 
R('Admin://User/showName') // Call  Admin  Project  User  Modular  showName  Method 
R('Admin/User/showName') // Call  Admin  Grouping  User  Modular  showName  Method 
R('Admin://Tool/User/info') // Call  Admin  Project  Tool  Grouped  User  Modular  info  Method 
R  Method receives parameters 
R  Method also supports passing in parameters to the called method, because in fact, the transferred operation may need to pass in parameters. 
R  The first of the method 2 Parameters are arrays that are passed in as parameters of the called operation. As shown in the following example: 
R( 'User/showName',array(5) );

This example shows that the showName operation will accept 5 as one parameter. The corresponding showName operation may be:


<?php
class UserAction extends Action{
 public function showName($id){
 //  According to  id  Parameter to get user information 
 }
}
?>

To pass in multiple parameters, define multiple elements of the array () parameter array of the R method in turn.

A method or R method?
From the above example, we can see that both A method and R method can call the operation of other modules. Is it better to use A method or R method? The suggestions are as follows: if you want to use multiple methods in other modules, it is recommended to use A method to call different methods of the module by object, so as to avoid instantiating objects many times; If you only need to use one of the methods in the other modules, then the R method is undoubtedly the most concise.

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "smarty Template Introduction Basic Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: