Home  >  Article  >  Backend Development  >  ThinkPHP R method example

ThinkPHP R method example

不言
不言Original
2018-06-06 14:18:222337browse

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

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

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

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

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

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

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 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!

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