How do we call across modules in thinkphp?
During the development process, methods of other modules are often called in the current module. This involves cross-module calls. We can also learn about the use of two shortcut methods, A and R.
$User = A("User"); // 实例化UserAction控制器对象 $User->importUser(); // 调用User模块的importUser操作方法
The A("User") here is a shortcut method, which is equivalent to the following code:
import("@.Action.UserAction"); $User = new UserAction();
In fact, in this example, there is a simpler call than the A method Methods, for example:
R("User","importUser"); // 远程调用UserAction控制器的importUser操作方法
The above is only called in the current project. If you need to call methods between multiple projects, the same can be done:
$User = A("User","App2"); // 实例化App2项目的UserAction控制器对象 $User->importUser(); // 远程调用App2项目的UserAction控制器的importUser操作方法 R("User","importUser","App2");
An example of mine:
A project is divided into two groups: admin and home
home is the group by default:
When instantiating the module (the current location is the IndexAction class in admin Instantiated in the index method)
import("@.Action.Home.UserAction"); $User=new UserAction(); $User->show(); $User->add();
Note: The method called must be public
Recommended tutorial: thinkphp tutorial
The above is the detailed content of thinkphp cross-module calling method. For more information, please follow other related articles on the PHP Chinese website!