Jump to content

Recommended Posts

Must PUT and DELETE be done through a form or is it possible to communicate via other methods? 

 

I ask because I've just begun playing with a framework that sets routing based on the request method and DELETE doesn't behave like i thought it did.  (passing the information to the method via link) :shrug:

Link to comment
https://forums.phpfreaks.com/topic/267399-http-delete-put-requests/
Share on other sites

Ok, I accept that.  However, let's venture down this path so I can continue my need-based edumacations.

 

Since the routing (and general need for this issue) revolves around Laravel and it's routing, I'll be using that code.  I'll also do my best to explain, since I don't expect anybody to research Laravel's docs to follow along.

 

Goal: Delete a database entry

 

Here's the route.  the static Route method can be one of the four request methods.  From there, it's the URI path and some other info, such as which controller/method to use. 

Route::delete('admin/module.uninstall', array( 'uses'=>'modules@uninstall'));

 

The controller method is named in a matching convention to the request method: public function delete_uninstall()

 

Now, using a form to submit data to the method works just fine.  Problem is, now i have a form button  :-[

 

So, instead of having a lame-duck form button (and potentially 30+ of them), i tried to use a regular html link.

<a href="admin/module.uninstall.module_slug">Uninstall</a>

 

And the route becomes

Route::delete('admin/module.uninstall.(:any)', array( 'uses'=>'modules@uninstall'));

 

Controller

delete_uninstall( $slug )

 

This, however, causes Laravel to shit bricks.  Converting the request method to GET for router / method produces desired effect.  But, as I'm learning something new I would like to get as best of an understanding as possible.

 

So the questions that remain:

[*] Should I maintain the DELETE routing with a form button because the class method is only used for deleting an entry - this is good semantics

[*] Is it possible to access this DELETE routing through any other method besides a form post

[*] Should I instead use a GET and act like it's all good?

A POSTed form has semantics that a regular link does not. Regardless of the underlying action, anything GETed (including links) represents a read-only request while anything POSTed (forms with method=post) can represent something with a lasting effect. Think about a web spider: it will crawl links while it will not submit forms.

So technically #1: you really should be using a form for a "delete"-type action. If you want the button to look like a link, that's a different problem.

 

As for why it doesn't work, there's something somewhere that indicates the form submission is a DELETE. I'm thinking two possibilities:

1. A hidden form field. This is one way MVC.NET does it.

2. The framework accepts a POST action in place of a DELETE, but not a GET.

I think the gears are turning now.

 

So it's all about the server request method that a form provices, whereas a URI request will always be a GET request as far as the webserver (not the framework) is concerned.  Hence, the failed server status.  Makes a lot of sense now that you point it out. 

 

Thanks, Req.  You're always spot on when I need it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.