Home > PHP Framework > ThinkPHP > How to use R method in thinkphp

How to use R method in thinkphp

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2022-02-25 11:45:38
Original
2542 people have browsed it

In thinkphp, the R method is used to call the operation method of a certain controller, which is a further enhancement and supplement of the A method. The calling format of the R method is "R('[project://][group /]Module/Operation','Parameters','Controller layer name')".

How to use R method in thinkphp

The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.

How to use R method in thinkphp

The R method is used to call the operation method of a certain controller, which is a further enhancement and supplement of the A method.

R method calling format:

R('[项目://][分组/]模块/操作','参数','控制器层名称')
Copy after login

For example, if we define an operation method as:

class UserAction extends Action {
public function detail($id){
return M('User')->find($id);
}
}
Copy after login

, then we can call this operation in other controllers through the R method Method (generally R methods are used for cross-module calls)

$data = R('User/detail',array('5'));
Copy after login

means calling the detail method of the User controller (the detail method must be of public type), and the return value is to query a user data with ID 5. If the operation method you want to call does not have any parameters, the second parameter can be left blank and used directly:

$data = R('User/detail');
Copy after login

It can also support calling across groups and projects, for example:

R('Admin/User/detail',array('5'));
Copy after login

Indicates calling the detail method of the User controller under the Admin group.

R('Admin://User/detail',array('5'));
Copy after login

indicates calling the detail method of the User controller under the Admin project.

The official recommendation is not to make too many calls on the same layer, which will cause logical confusion. The publicly called parts should be encapsulated into separate interfaces, which can be added separately with the help of the new feature of 3.1 multi-layer controller. A controller layer is used for interface calls. For example, we add an Api controller layer,

class UserApi extends Action {
public function detail($id){
return M('User')->find($id);
}
}
Copy after login

Then, use the R method to call

$data = R('User/detail',array('5'),'Api');
Copy after login

In other words, the third parameter of the R method Supports the controller layer of specified calls.

At the same time, the R method can support the operation suffix setting C ('ACTION_SUFFIX') when calling the operation method. If you set the operation method suffix, you still do not need to change the calling method of the R method.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use R method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
ThinkPHP Why use composer?
From 1970-01-01 08:00:00
0
0
0
thinkphp versions supported by php6
From 1970-01-01 08:00:00
0
0
0
thinkphp upload files
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template