无法使用 Slim Framework 从 DELETE 请求中获取 body
Cannot get body from DELETE request with Slim Framework
我正在使用 backbone.js 和 Slim Framework 开发应用程序。
问题在这里:我需要通过执行一个 MySQL 存储过程来删除一条记录,该存储过程除了要删除的记录的 ID 外,还接收另一个参数来执行一些业务逻辑。
问题是我只收到记录的 ID。我的 $app->request()->getBody();是空白的。
这是代码:
/rest/folder/index.php
<?php
require_once '../../vendors/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app -> get('/categories/', function() use ($app) {
require_once ('../../libs/auth.inc');
$auth = new Authentication();
$user_id = $auth -> SessionAuthenticate();
require_once ('categories.php');
$categories = new Categories();
$categories -> getCategories($app, $user_id);
});
$app -> post('/categories/', function() use ($app) {
require_once ('../../libs/auth.inc');
$auth = new Authentication();
$user_id = $auth -> SessionAuthenticate();
require_once ('categories.php');
$categories = new Categories();
$categories -> saveCategory($app, $user_id);
});
$app -> put('/categories/:category_id', function($category_id) use ($app) {
require_once ('../../libs/auth.inc');
$auth = new Authentication();
$user_id = $auth -> SessionAuthenticate();
require_once ('categories.php');
$categories = new Categories();
$categories -> saveCategory($app, $user_id);
});
$app -> delete('/categories/:category_id', function ($category_id) use ($app) {
require_once ('../../libs/auth.inc');
$auth = new Authentication();
$user_id = $auth -> SessionAuthenticate();
require_once('categories.php');
$categories = new Categories();
$categories -> deleteCategory($app, $user_id, $category_id);
});
$app -> run();
这是我在 /rest/folder/categories 上的删除功能。php
public function deleteCategory($app, $user_id,$category_id) {
$requestBody = $app->request()->getBody();
$params = json_decode($requestBody, true);
$agreement_id = $params['agreement_id'];
$con = $this->conectarDB();
$query = "CALL SPD_CATEGORIES($category_id,$agreement_id);";
//print_r($query);
$result = $this->con->query($query);
$this->con->close();
$app->status(200);
echo json_encode($result);
}
这是我在 /modules/folder/CategoriesView.js
上的 backbone 删除功能
confirmDeleteForm : function(event) {
event.preventDefault();
var category_id = $('#categories_select').select2("val");
var self=this;
this.category = this.categories.findWhere({'category_id':category_id});
var agreement_id = this.category.get('agreement_id');
$('#deleteModal').modal('hide');
this.category.destroy({
data: {
'agreement_id': agreement_id,
},
processData : true,
success: function(model, response) {
self.cleanForm();
self.getCategories();
},
error: function(model, response) {
if (response.status == 500) {
self.showErrorMessage('Se produjo un error en el servidor', 'ERROR');
} else {
self.showErrorMessage(response.responseText, 'ATENCION');
}
}
});
},
在模型销毁中,我尝试添加数据:和 HEADERS:但是 none 的工作。
可以制作吗?
提前致谢。
更新 1:
我找到了这个:Slim v2
如第一个答案所述。
还有这个:Backbone.js(BACKBONE.ROUTER 标题)
这就是我现在正在尝试的。我真的可以开始工作了,但我认为这是解决我遇到的问题的方法。
更新 2
仍然可以弄清楚如何做到这一点。因此,由于我的 almost-here 截止日期,我将数据库结构更改为仅使用行 ID 进行删除。
完成项目后我会继续尝试这个。
谢谢。
你读过这个吗:http://docs.slimframework.com/routing/delete/#method-override?
Unfortunately, modern browsers do not provide native support for HTTP
DELETE requests.
...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.
我正在使用 backbone.js 和 Slim Framework 开发应用程序。 问题在这里:我需要通过执行一个 MySQL 存储过程来删除一条记录,该存储过程除了要删除的记录的 ID 外,还接收另一个参数来执行一些业务逻辑。 问题是我只收到记录的 ID。我的 $app->request()->getBody();是空白的。 这是代码:
/rest/folder/index.php
<?php
require_once '../../vendors/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app -> get('/categories/', function() use ($app) {
require_once ('../../libs/auth.inc');
$auth = new Authentication();
$user_id = $auth -> SessionAuthenticate();
require_once ('categories.php');
$categories = new Categories();
$categories -> getCategories($app, $user_id);
});
$app -> post('/categories/', function() use ($app) {
require_once ('../../libs/auth.inc');
$auth = new Authentication();
$user_id = $auth -> SessionAuthenticate();
require_once ('categories.php');
$categories = new Categories();
$categories -> saveCategory($app, $user_id);
});
$app -> put('/categories/:category_id', function($category_id) use ($app) {
require_once ('../../libs/auth.inc');
$auth = new Authentication();
$user_id = $auth -> SessionAuthenticate();
require_once ('categories.php');
$categories = new Categories();
$categories -> saveCategory($app, $user_id);
});
$app -> delete('/categories/:category_id', function ($category_id) use ($app) {
require_once ('../../libs/auth.inc');
$auth = new Authentication();
$user_id = $auth -> SessionAuthenticate();
require_once('categories.php');
$categories = new Categories();
$categories -> deleteCategory($app, $user_id, $category_id);
});
$app -> run();
这是我在 /rest/folder/categories 上的删除功能。php
public function deleteCategory($app, $user_id,$category_id) {
$requestBody = $app->request()->getBody();
$params = json_decode($requestBody, true);
$agreement_id = $params['agreement_id'];
$con = $this->conectarDB();
$query = "CALL SPD_CATEGORIES($category_id,$agreement_id);";
//print_r($query);
$result = $this->con->query($query);
$this->con->close();
$app->status(200);
echo json_encode($result);
}
这是我在 /modules/folder/CategoriesView.js
上的 backbone 删除功能confirmDeleteForm : function(event) {
event.preventDefault();
var category_id = $('#categories_select').select2("val");
var self=this;
this.category = this.categories.findWhere({'category_id':category_id});
var agreement_id = this.category.get('agreement_id');
$('#deleteModal').modal('hide');
this.category.destroy({
data: {
'agreement_id': agreement_id,
},
processData : true,
success: function(model, response) {
self.cleanForm();
self.getCategories();
},
error: function(model, response) {
if (response.status == 500) {
self.showErrorMessage('Se produjo un error en el servidor', 'ERROR');
} else {
self.showErrorMessage(response.responseText, 'ATENCION');
}
}
});
},
在模型销毁中,我尝试添加数据:和 HEADERS:但是 none 的工作。
可以制作吗?
提前致谢。
更新 1:
我找到了这个:Slim v2 如第一个答案所述。
还有这个:Backbone.js(BACKBONE.ROUTER 标题)
这就是我现在正在尝试的。我真的可以开始工作了,但我认为这是解决我遇到的问题的方法。
更新 2
仍然可以弄清楚如何做到这一点。因此,由于我的 almost-here 截止日期,我将数据库结构更改为仅使用行 ID 进行删除。 完成项目后我会继续尝试这个。
谢谢。
你读过这个吗:http://docs.slimframework.com/routing/delete/#method-override?
Unfortunately, modern browsers do not provide native support for HTTP DELETE requests.
...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.