简体   繁体   中英

Cannot get body from DELETE request with Slim Framework

I'm working in an app using backbone.js and Slim Framework. The issue is here: I need to delete a record by executing a MySQL stored procedure that receives, besides the ID of the record to delete, another parameter to execute some business logic. The problem is that I only receive the ID of the record. My $app->request()->getBody(); is blank. Here is the code:

/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();

Here is my delete function on /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);
}

And here is my backbone delete function on /modules/folder/CategoriesView.js

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');
            }
        }
    });

},

In the model destroy I tried to add DATA: and HEADERS: but none of the work.

Is it possible to be made?

Thanks in advance.

UPDATE 1:

I've found this: Slim v2 as referred in first answer.

and this: Backbone.js (BACKBONE.ROUTER title)

This is what I'm trying now. I really can get to work, but I think this is the way to resolve the issue I'm having.

UPDATE 2

Still can figure out how to do this. So, because of my almost-here deadline, I changed the database structure to only delete with a row ID. I will keep trying this after finishing the project.

Thanks.

Have you read this: 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.

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