xmgravity's avatar

Use POST request for update routes

I've been using POST request for update routes and it's working just as well, but most people are saying that we should use PUT/PATCH request for update routes.

So let's say the update function is like below:

public function update(Request $request, $id)
{
    $input = $request->all();
    $model = App\Model::find($id);
    $model->update($input);
}

Apart from the semantics, is there any difference at all between using POST and PUT/PATCH for that function?

0 likes
5 replies
Snapey's avatar
Snapey
Best Answer
Level 122

No, Just semantics. It does not change the behaviour at all.

POST should be to create a new resource, and a POST is expected to contain all data required to create the resource

PATCH is to change an attribute or multiple attributes of an existing resource.

PUT is to update an existing resource or replace ALL attributes, but like POST you are expected to provide all the fields required.

But really, its up to you how much you want to follow convention

2 likes
mushood's avatar

While its just semantics like Snapey said, when people use your update routes, they will look for put/patch. If they can't find it, they will figure out its the post route. Conventions just make things easier to understand when working in group.

AddWebContribution's avatar

POST

HTTP.POST can be used when the client is sending data to the server and the server will decide the URI for the newly created resource. The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

PUT

HTTP.PUT can be used when the client is sending data to the server and the client is determining the URI for the newly created resource. The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

PATCH

HTTP.PATCH can be used when the client is sending one or more changes to be applied by the server. The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI. The set of changes is represented in a format called a patch document.

xmgravity's avatar

@SNAPEY - Is it the same with DELETE as well? I also often use POST for DELETE routes as well, so I mostly only use GET and POST in my route files.

I do have a naming convention such as getIndex, getCreate, postCreate, getEdit, postEdit, postDelete so I know whether the function is using what request.

Snapey's avatar

well I would say, definately no, always use Delete

Please or to participate in this conversation.