I'm making a REST API in PHP and I understand that I can capture the request method via $_SERVER['REQUEST_METHOD']
.
However, how do I trigger PUT/DELETE requests in the browser?
I can't imagine changing the method attribute of the form tag to specify a method other than GET or POST would work on all browsers...plus the HTML standard doesn't recognize it.
Thanks.
Usually this is done by a hidden form field, and handled in the application (not the webserver, by and large).
<input type="hidden" name="_method" value="put" />
So in this case you'd use a simple set of if-else statements to determine if the _method
variable is overridden (validly, of course). I'd use something like:
$method = 'get';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['_method']
&& ($_POST['_method'] == 'PUT' || $_POST['_method'] == 'DELETE')) {
$method = strtolower($_POST['_method']);
} else {
$method = 'post';
}
}
This would be a simple way to determine the request type for your application or framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With