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')".
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)
