Laravel API资源如何调用特定方法 [英] Laravel apiResources how to call specific method

查看:218
本文介绍了Laravel API资源如何调用特定方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel apiResource,但是显然有些用法我无法理解. 在simpe路线中,您会遇到类似的情况

I am trying to use Laravel apiResource but obviously there is something that I can't understand of the usage. In a simpe route you have something like that

    Route::get('user/{id}', 'UserController@show');

其中显示是您要调用的方法.

where show is the method you want to call.

如何指定要在apiResource中调用的方法?

How can I specify which method to be called in apiResource?

目前我有

  Route::apiResources(['user' =>'API\UserController']);

UserController 中调用 store 方法.我想指定另一个要调用的方法,例如

which call the store method in the UserController. I would like to specify another method to be called but for example

 Route::apiResources(['user' =>'API\UserController@show']);

不会调用 show 方法

那么我该如何指定要在apiResources中调用的方法呢?

So how can I specify which method to be called in apiResources?

推荐答案

对于资源,保留showstoreupdateindex方法.这是它的工作方式:

For a resource, show, store, update, index methods are reserved. Here is how it is going to work:

  • 没有任何ID的路由上的GET调用将调用index()方法
  • 具有资源ID的路由上的GET调用将调用show()方法
  • 该路线上的POST调用将调用store()方法
  • 具有ID的路由上的PATCH调用将调用update()方法
  • A GET call on the route without any ID will call the index() method
  • A GET call on the route with a resource ID will call the show() method
  • A POST call on the route will call the store() method
  • A PATCH call on a route with ID will call the update() method

API资源路由与网络资源路由相同,只是它不包含返回视图的方法,即create()edit()等.

The API resource route is identical to the web resource route except it doesn't come with the methods that return the views, i.e. create(), edit() etc.

如果需要任何自定义路由,则需要先定义所需的路由,然后再定义API资源路由.我希望这有帮助. 此处的官方文档.

If you need any custom routes, you need to define the needed routes before defining the API resource route. I hope this helps. More info is present in the official docs here.

这篇关于Laravel API资源如何调用特定方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆