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

How to use R method in thinkphp

WBOY
WBOYOriginal
2022-02-25 11:45:382376browse

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('[项目://][分组/]模块/操作','参数','控制器层名称')

For example, if we define an operation method as:

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

, 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'));

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');

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

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

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

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

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);
}
}

Then, use the R method to call

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

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!

Statement:
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