简体   繁体   中英

Issue with DELETE method- PHP slim framework

I have issue with DELETE method. Below is the code, where i am getting 404 error for DELETE method. If i use get method instead of delete , code executing properly. Please help me where i went wrong.

<?php
require "Slim/Slim.php";
require "NotORM/NotORM.php";
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$pdo = new PDO('mysql:host=localhost;dbname=rest_trial', 'uname', 'passwd');
$db = new NotORM($pdo);

//DELETE method
$app->delete("/deletebook/:id", function ($id) use($app, $db) {
//$app->get("/deletebook/:id", function ($id) use($app, $db) {
$app->response()->header("Content-Type", "application/json");
$book = $db->books()->where("id", $id);
if ($book->fetch()) {
    $result = $book->delete();
    echo "Book deleted successfully";
}
else{
    echo "Book id $id does not exist";
}
});
$app->run();

If you look in the network tab of your browser console you can check if the method of your request is actually DELETE .

Some browsers do not support PUT and DELETE http request method. Slim provide a method for that:

" Unfortunately, modern browsers do not provide native support for HTTP DELETE requests. To work around this limitation, ensure your HTML form's method attribute is “post”, then add a method override parameter to your HTML form like this: "

<form action="/books/1" method="post">
    ... other form fields here...
    <input type="hidden" name="_METHOD" value="DELETE"/>
    <input type="submit" value="Delete Book"/>
</form>

" If you are using Backbone.js or a command-line HTTP client, you may also override the HTTP method by using the X-HTTP-Method-Override header. "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM