This article mainly introduces the R method of ThinkPHP. For more information, see ThinkPHP.
The R method of ThinkPHP is used to call the operation method of A controller, which is further enhanced and supplemented by Method.
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); } }
Then 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'));
It indicates that the detail method of the User controller is called (the detail method must be of the public type), and the return 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');
Cross-group and Project calls are also supported, for example:
R('Admin/User/detail',array('5'));
Call the detail method of the User controller under the Admin group.
R('Admin://User/detail',array('5'));
Call the detail method of the User controller under the Admin project.
The official recommendation is not to make too many calls at the same layer. Otherwise, the logic may be disordered. The public call part should be encapsulated into a separate interface. you can use the new ThinkPHP3.1 multi-layer controller, 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); } }
Then, use the R method to call
$data = R('User/detail',array('5'),'Api');
That is to say, 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.