The method of operation of the R method for invoking a controller is further enhanced and supplemented by the a approach. See here for the usage of a method.
The call format for the R method:
R (' [Project://][Group/] Module/operation ', ' parameters ', ' Controller layer name ')
For example, we define an action method that:
- class UserAction extends Action {
- public function detail($id){
- return M(‘User‘)->find($id);
- }
- }
复制代码
Then you can call this operation method in the other controller through the R method (the general R method is used for cross-module invocation)
- $data = R(‘User/detail‘,array(‘5‘));
复制代码
Represents the detail method that invokes the user controller (the detail method must be of the public type), and the return value is a user data with a query ID of 5. If the action method you want to invoke is not a parameter, the second argument can be left blank and used directly:
- $data = R(‘User/detail‘);
复制代码
You can also support cross-grouping and project invocation, for example:
- R(‘Admin/User/detail‘,array(‘5‘));
复制代码
Represents the detail method that invokes the user controller under the Admin group.
- R(‘Admin://User/detail‘,array(‘5‘));
复制代码
Represents the detail method that invokes the user controller under the Admin project.
The official recommendation is not to have too many calls on the same layer, which can cause logical confusion, the part that is called by the public should be encapsulated as a separate interface, with a new 3.1-feature multilayer controller that adds a single controller layer for interface calls, for example, we add an API controller layer,
- class UserApi extends Action {
- public function detail($id){
- return M(‘User‘)->find($id);
- }
- }
复制代码
Then, using the R method call
- $data = R(‘User/detail‘,array(‘5‘),‘Api‘);
复制代码
That is, the third parameter of the R method supports the controller layer that specifies the call.
At the same time, the R method calls the action method can support the operation suffix set C (' Action_suffix '), if you set the action method suffix, still do not need to change the method of call R methods.
thinkphp Function: R method