Backbone.js的如何使用PHP中使用 [英] Backbone.js How to use with PHP

查看:84
本文介绍了Backbone.js的如何使用PHP中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找到Backbone.js的,我似乎无法弄清楚如何得到它用PHP,以保存模型的数据通信。它发送一个请求,但我怎么捕捉到的请求是否被创建,更新,读,删除等。

I have been looking into backbone.js and I can't seem to figure out how to get it communicate with php in order to save the models data. It sends a request but how do I capture that request whether it be "Create", "Update", "Read", "Delete" etc.

感谢

推荐答案

您可以考虑另一种选择是使用具有全部内置于执行你的主干服务器查询所需的功能pre封装的RESTful框架滚动。我个人最喜欢的是乔希洛克哈特的SlimPHP框架

Another option you may consider is to roll with a pre-packaged RESTful framework that has all the necessary functions built in to execute your Backbone server queries. My personal favorite is Josh Lockhart's SlimPHP Framework.

一些简单的示例code(一旦你有SlimPHP设置)来把你的电话骨干看起来是这样的。

Some simple sample code (once you have SlimPHP setup) used to take your Backbone calls look like this.

$app->get('/user', function() use ($app) {

    // See if session is set, get user info as array
    if (isset($_SESSION['userID']) {
         $user = // grab user with userID data from DB
    }

    // Respond to the get request with a json object
    $response = $app->response;
    $response['Content-Type'] = 'application/json';
    $response->body(json_encode($user));
}

下面是一个POST的例子,轮流骨干JSON到数组。

Here is a POST example that turns Backbone json into arrays.

// Middleware that detects type of data and converts it to something usable
$app->add('Slim_Middleware_ContentTypes');    // JSON to associative array

...

$app->post('/message', function() use ($app) {
    $dataIn = $app->request()->getBody();

    ...

    // Save to DB $dataIn['message'], $dataIn['author'], etc.
}

下面是使用一些参数的PUT的例子。

Here is a PUT example using some parameters.

$app->put('/user/:id', function($id) use ($app) {

    // Find appropriate user from DB that has $id as ID

    $dataIn = $app->request()->getBody();

    // Save to DB $dataIn['name'], $dataIn['age'], etc.
}

这里是一个DELETE。

And here is a DELETE.

$app->delete('/message/:id', function($id) use ($app) {

    // Find appropriate message from DB that has $id as ID

    // Delete message with id of $id
}

虽然这不是所有其他的事情详尽的例子来考虑,它应该给你的各种开放式解决方案的想法已经在那里供您使用。我个人比较喜欢苗条,因为它是如此轻巧,操作简单,但它拥有所有你想要在一个RESTful服务器的功能。伟大的原型。用DB抽象层和一些其他的工具结合起来,你可以让你想更快的任何事情。

While this isn't an exhaustive example of all the other things to consider, it should give you an idea of the kinds of open solutions already out there for you to use. I personally like Slim because it is so lightweight, simple, yet it has all the features you'd want in a RESTful server. Great for prototyping. Combine it with a DB abstraction layer and some other tools and you can make just about anything you want quicker.

您可以看到这里沿着这些线路其他一些样品code:

You can see some other sample code along these lines here:


  1. How张贴骨干模型服务器

  2. Ways节省骨干数据

  1. How to post Backbone model to server
  2. Ways to save Backbone data

这是一些其他的基于PHP的RESTful的解决方案的链接:<一个href=\"http://blog.programmableweb.com/2011/09/23/short-list-of-restful-api-frameworks-for-php/\">Framework列表

And here is a link to some other PHP based RESTful solutions: Framework List

这篇关于Backbone.js的如何使用PHP中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆