【问题标题】:What are REST standards for PUT and POST methods?PUT 和 POST 方法的 REST 标准是什么?
【发布时间】:2019-04-10 12:04:08
【问题描述】:

我在 Laravel 中实现 API,并收到评论说我的 POST 和 PUT 方法根据 REST 标准不准确。

我使用 POST 创建新资源,使用 PUT 更新现有资源。看不到问题。

端点:

Route::post('/cities', [
    'uses' => 'CityController@store'
]);

Route::put('/cities/{id}', [
    'uses' => 'CityController@update'
]);

PUTPOST 方法:

public function update(Request $request, $id)
{
    $this->validate($request, [
        'name'      => 'required|min:3',
        'latitude'  => 'required|numeric',
        'longitude' => 'required|numeric'
    ]);

    // update model and only pass in the fillable fields
    $this->cityRepository->update(
        $request->only($this->cityRepository->getModel()->fillable), $id
    );

    return $this->cityRepository->show($id);
}

public function store(Request $request)
{
    $this->validate($request, [
        'name'      => 'required|min:3',
        'latitude'  => 'required|numeric',
        'longitude' => 'required|numeric'
    ]);

    $data = $this->cityRepository->create(
        $request->only($this->cityRepository->getModel()->fillable));

    if ($data) {
        $message = self::SUCCESSFULLY_CREATED;
        $code = self::HTTP_CODE_CREATED;
    } else {
        $message = self::UNSUCCESSFULLY_CREATED;
        $code = 409;
    }

    return $this->sendResponse($message, $data, $code);
}

发送响应:

public function sendResponse($message, $result = [], $code = 200)
    {
        $response = [
            'message' => $message,
        ];

        if (!empty($result)) {
            $response['data'] = $result;
        }

        return response()->json($response, $code);
    }

显示方法:

 public function show($id)
    {
        return $this->model->findOrFail($id);
    }

【问题讨论】:

  • 谁说创建的 POST 和更新的 PUT 不符合 REST 标准?
  • 并不是说根据 REST 标准创建的 POST 和要更新的 PUT 不准确,而是我的方法没有根据 REST 标准准确实施。我不明白为什么。
  • 嗯,我能看到的一件事有点不对劲,那就是发送 409 响应代码。 409 表示“冲突”,但在这里你真的不知道原因是否是“冲突”,可能是数据库已关闭,在这种情况下没有冲突。除此之外,它可能还取决于sendResponseshow 的实现方式。
  • 我从sendResponseshow 方法添加代码。是的,在这种情况下,409 可能不适合返回。
  • 当您创建资源时,您的响应应该是 201,如果我没记错的话,结果应该是新创建的资源而不是消息。

标签: laravel rest api post put


【解决方案1】:

您可以从 store 方法而不是 SUCCESSFULLY_CREATED 返回创建的对象。除此之外,代码看起来不错。

看看https://laravel.com/docs/5.8/controllers#resource-controllers 上的表格,它有一个相当有用的各种 CRUD 路由的 REST 定义:

GET         /photos                 index       photos.index
GET         /photos/create          create      photos.create
POST        /photos                 store       photos.store
GET         /photos/{photo}         show        photos.show
GET         /photos/{photo}/edit    edit        photos.edit
PUT/PATCH   /photos/{photo}         update      photos.update
DELETE      /photos/{photo}         destroy     photos.destroy

这里有一个很好的资源,您应该返回哪些 HTTP 方法:

https://www.restapitutorial.com/lessons/httpmethods.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-28
    • 2011-04-22
    • 2013-08-26
    • 2016-04-03
    • 2020-06-19
    • 2019-07-26
    • 1970-01-01
    • 2012-07-18
    相关资源
    最近更新 更多