thinkphp Function: R method

Source: Internet
Author: User

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:

    1. class UserAction extends Action {
    2. public function detail($id){
    3. return M(‘User‘)->find($id);
    4. }
    5. }

复制代码

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)

    1. $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:

    1. $data = R(‘User/detail‘);

复制代码

You can also support cross-grouping and project invocation, for example:

    1. R(‘Admin/User/detail‘,array(‘5‘));

复制代码

Represents the detail method that invokes the user controller under the Admin group.

    1. 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,

    1. class UserApi extends Action {
    2. public function detail($id){
    3. return M(‘User‘)->find($id);
    4. }
    5. }

复制代码

Then, using the R method call

    1. $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

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.