Home > Backend Development > PHP Tutorial > ThinkPHP R method example

ThinkPHP R method example

不言
Release: 2023-03-30 07:28:01
Original
2382 people have browsed it

This article mainly introduces the R method of ThinkPHP. Friends who need it can refer to it

The R method of ThinkPHP 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('[Project://][Group/]Module/Operation','Parameter','Controller layer name')

For example, we define an operation method as:

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

Then it can be called in other controllers through the R method This operation 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 a public type) and returns The value is to query 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

can also support cross-group and Project call, for example:

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

means 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, otherwise it will cause logical confusion. The publicly called parts should be encapsulated into separate interfaces, and you can use the new feature of ThinkPHP3.1 multi-layer controller. , add a separate 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);
  }
 }
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 specifying the controller layer of the call.
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.

Related recommendations:

ThinkPHP user registration login message complete example

##

The above is the detailed content of ThinkPHP R method example. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template