ThinkPHP R method example _ PHP-php Tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.