How to POST backbone model data to DB through Slim php and Paris How to POST backbone model data to DB through Slim php and Paris json json

How to POST backbone model data to DB through Slim php and Paris


I came up with a solution to completing the problem: how to get data from client to server using the default backbone save() and .sync - passed over to the Slim php framework and going through Paris/Idiorm to my DB.

I am including my working updated code below:

Client-side: Backbone.js

var Donut = Backbone.Model.extend({    defaults: {        name: null,        sparkles: false,        creamFilled: false    },    url: function() {        return '/donut';    }});var bostonCream = new Donut({    name: 'Bawston Cream',    sparkles: true,    creamFilled: true});bostonCream.save();/***** If you want to check out the response to save() ? ***bostonCream.save({}, {    success: function(model, response) {        console.log('SUCCESS:');        console.log(response);    },    error: function(model, response) {        console.log('FAIL:');        console.log(response);    }});************************************************************/

Sever-side: Slim PHP w/ Paris/Idorm

class Donut extends Model {}$app->post('/donut', function() use ($app) {    $donuts = Model::factory('Donut')->create();    /* EDIT: Works... but not the Slim way    $parameters = json_decode(file_get_contents('php://input'), true);    $donuts->name = $parameters['name'];    $donuts->sparkles = $parameters['sparkles'];    $donuts->creamFilled = $parameters['creamFilled']; */    /* SLIM: Using Slim Request Object */    $requestBody = $app->request()->getBody();  // <- getBody() of http request    $json_a = json_decode($requestBody, true);    $donuts->name = $json_a['name'];    $donuts->sparkles = $json_a['sparkles'];    $donuts->creamFilled = $json_a['creamFilled'];    $donuts->save();    // echo json_encode($parameters); // Prove you've captured POST data, send it back}

Now my code is happily using the default settings of Backbone.js (no changes to sync) and sending proper model attribute information to my server which seems to be successfully accepting the data and saving it to my DB.

The key here seems to be this line...

/* $parameters = json_decode(file_get_contents('php://input'), true); */// EDITED: getBody() method not documented in Develop Doc, only Stable @ time of post$requestBody = $app->request()->getBody();


If you want to know "what exactly is sent to the server", you should have a look at the Backbone.sync function in Backbone's code. It is very well documented, step-by-step. Then, the cleanest way to achieve what you need is to write you own sync function, inspired by Backbone's sync.

Also, a quick way to see what is sent to the server is to use your browser debug console (Network tab). You can compare here what is sent by Backbone vs. what is sent when you use $.post directly. Please post this information if you need more help !


backbone sends json data to your php backend server, which you should expose your RESTful api to respond to http verb like get, post, put, delete and etc.

your backend api is responsible for communicating with database.

I am not sure about SLIM PHP. it seems to handle the request. Can you paste the error messages?