This article mainly introduces about thinkphp implementation of cross-module calls, has a certain reference value, now share to everyone, the need for friends can refer to
Using $this can invoke methods within the current module, but in many cases the methods of other modules are often called in the current module. This article mainly describes the thinkphp cross-module call operation, the need for friends can refer to the following
Using $this can invoke methods within the current module, but in many cases the methods of other modules are often called in the current module. The thinkphp incorporates a method with the R method, two special uppercase methods to handle the problem of cross-module invocation.
At present index module has the index Operation user module has showname operation user module and SHOWNAME operation specific code as follows:
<?phpclass Useraction extends action{public Function showname () {echo ' Hello World ';}}? >
We will invoke the above ShowName operation in the index operation of the index module.
Invoke operations across modules with A method
The A method is used to instantiate other modules (when in the New keyword), and after the module is instantiated, the action within the module can be invoked as an object.
Invoke the ShowName action instance of the User module in the index operation of the index module:
<?phpclass Indexaction extends action{public Function index () {header ("content-type:text/html; Charset=utf-8 "); Instantiate the user module $User = A (' User '); Call the method in the User module $User->showname (); }}?>
The A method also supports cross-group calls and cross-project invocation modules with the following syntax:
A (' < project name://>< Group name/> Module name ')
A common example is a (' User ')//Call the user module of the current project, such as the above example a (' Admin://user ')//Call the Admin Project user module A (' Admin/user ')//Call the Admin Group user module A (' Admin://tool/user ')//Call the Admin Project Tool Group User module
Invoke actions across modules through the R method
The thinkphp also provides an R method that directly invokes the action method of other modules , changing the example above using the A method to the R method:
<?phpclass Indexaction extends action{public Function index () {header ("content-type:text/html; Charset=utf-8 "); Call the method R (' User/showname ') in the User module; }}?>
The R method also supports cross-group calls and cross-project invocation methods, with the following syntax:
R (' < project name://>< Group name/> Module name/operation ' <, array () >)
Common examples are the following R (' User/showname ')//Call the ShowName method of the user module of the current project, such as the above example R (' Admin://user/showname ')//Call the Admin Project user module Shownam E Method R (' Admin/user/showname ')//Call Admin Group User module ShowName method R (' Admin://tool/user/info ')//Call Admin Project Tool Group User module The Info method r method Receive parameter R method also supports passing parameters to the called method, because the operation that is actually mobilized may be required to pass in the parameter. The second parameter of the R method is an array that is passed in as a parameter to the invoked operation. As shown in the following example: R (' User/showname ', Array (5));
This example indicates that the ShowName operation will accept 5 such a parameter. The corresponding showname operation may be:
<?phpclass Useraction extends action{public function showname ($id) {//code to get user information by ID parameter}}?>
To pass in more than one parameter, the array () parameter arrays of the R method are defined in turn for multiple elements.
The A method or the R method?
As can be seen from the above example, both the A method and the R method can invoke the operation of other modules, is it better to use a method or R method? Here are some suggestions: If you want to use multiple methods within other modules, it is recommended that you use the A method to invoke different methods of the module through an object, avoid instantiating the object multiple times, and if you only need to use one of the other modules, the R method is the most concise .