The R method is used to call the operation method of A controller. it is further enhanced and supplemented by Method. For the usage of Method A, see here. Call Format of the R method: R ('[Project: //] [group/] module/operation', 'parameters', 'Controller layer name') for example, we have defined an operation method: the R method is used to call the operation method of A controller, which is further enhanced and supplemented by Method. For the usage of Method A, see here.
Call Format of the R method:
R ('[ project: //] [group/] module/operation', 'parameters', 'Controller layer name ')
For example, we define an operation method as follows:
- Class UserAction extends Action {
- Public function detail ($ id ){
- Return M ('user')-> find ($ id );
- }
- }
Copy the code so you can call this operation method in other controllers using the R method (generally, the R method is used for cross-module calls)
- $ Data = R ('user/detail', array ('5 '));
Copy the code to call the detail method of the User controller (the detail method must be of the public type). the returned value is to query a User data whose id is 5. If the operation method you want to call does not have any parameters, you can leave the second parameter blank and use it directly:
- $ Data = R ('user/detail ');
Code replication also supports cross-group and Project calls, for example:
- R ('admin/User/detail', array ('5 '));
Copy the code to call the detail method of the User controller under the Admin group.
- R ('admin: // User/detail', array ('5 '));
Copy the code to call the detail method of the User controller under the Admin project.
The official suggestion is not to call too many on the same layer, which may cause logical confusion. The public call part should be encapsulated into a separate interface, and the new features of 3.1 can be used for multi-layer controllers, add a controller layer for interface calling. for example, we add an Api controller layer,
- Class UserApi extends Action {
- Public function detail ($ id ){
- Return M ('user')-> find ($ id );
- }
- }
Copy the code and call it using the R method.
- $ Data = R ('user/detail', array ('5'), 'api ');
Copy the code, that is, the third parameter of the R method supports specifying the called controller layer.
At the same time, when the R method calls the operation method, you can set the operation suffix c ('Action _ SUFFIX '). If you have set the operation method SUFFIX, you do not need to change the calling method of the R method.