javascript Backbone.js 如何与 PHP 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6207286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:51:27  来源:igfitidea点击:

Backbone.js How to use with PHP

phpjavascriptbackbone.js

提问by brenjt

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.

我一直在研究backbone.js,但似乎无法弄清楚如何让它与php通信以保存模型数据。它发送一个请求,但我如何捕获该请求,无论它是“创建”、“更新”、“读取”、“删除”等。

Thanks

谢谢

采纳答案by jmk2142

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.

您可能会考虑的另一个选择是使用预打包的 RESTful 框架,该框架内置了所有必要的功能来执行您的 Backbone 服务器查询。我个人最喜欢的是Josh Lockhart 的 SlimPHP Framework

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

一些用于处理 Backbone 调用的简单示例代码(一旦您安装了 SlimPHP 后)如下所示。

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

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

这是一个将 Backbone json 转换为数组的 POST 示例。

// 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.
}

Here is a PUT example using some parameters.

这是一个使用一些参数的 PUT 示例。

$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.
}

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
}

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.

虽然这不是要考虑的所有其他事情的详尽示例,但它应该让您了解已经存在的可供您使用的开放解决方案的种类。我个人喜欢 Slim,因为它非常轻巧、简单,但它具有 RESTful 服务器所需的所有功能。非常适合原型制作。将它与数据库抽象层和一些其他工具结合起来,您可以更快地制作任何您想要的东西。

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

您可以在此处查看其他一些示例代码:

  1. How to post Backbone model to server
  2. Ways to save Backbone data
  1. 如何将 Backbone 模型发布到服务器
  2. 保存 Backbone 数据的方法

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

这里是一些其他基于 PHP 的 RESTful 解决方案的链接:框架列表

回答by Flyingmana

The model of backbone.js uses specific urls for fetch and send data. You have to make sure, to have a php script called there.

Backbone.js 的模型使用特定的 url 来获取和发送数据。您必须确保在那里调用 php 脚本。

Now there are two possibilities.

现在有两种可能。

First, the method is send inside the $_POST as additional variable. Second, you have to look for the used request method(GET,POST,PUT,DELETE) which you can with $_SERVER['REQUEST_METHOD']

首先,该方法作为附加变量在 $_POST 中发送。其次,您必须寻找可以使用的已使用请求方法(GET,POST,PUT,DELETE)$_SERVER['REQUEST_METHOD']

Now you can use simple if-else or switch statements to handle the request and deliver the needed data as i think json.

现在您可以使用简单的 if-else 或 switch 语句来处理请求并像我认为的 json 一样传递所需的数据。

回答by Gabriel

$GLOBALS['HTTP_RAW_POST_DATA'] works fine for me, i don't know for what reason print_r($_POST) doesn't print anithing!!

$GLOBALS['HTTP_RAW_POST_DATA'] 对我来说很好用,我不知道什么原因 print_r($_POST) 不打印!

回答by herohat

In your php script you will have this for the PUT and DELETE methods as you can filter those with: $_SERVER['REQUEST_METHOD']

在您的 php 脚本中,您将拥有 PUT 和 DELETE 方法,因为您可以使用以下方法过滤它们: $_SERVER['REQUEST_METHOD']

parse_str(file_get_contents("php://input"),$post_vars);

If the request is POST or GET you can use it normally but if the request is PUT or DELETE so use the above line and so you can access to the vars with:

如果请求是 POST 或 GET,您可以正常使用它,但如果请求是 PUT 或 DELETE,那么请使用上面的行,这样您就可以使用以下方式访问变量:

$post_vars['fruit'] for example...

When you are ready to answer to backbone, you only have to first make the correct header:

当你准备回答主干时,你只需要先做出正确的标题:

header('Content-type: application/json; charset=utf-8');

and encode you answer with json:

并用 json 编码你的答案:

echo json_encode(array("message"=>"missing fields","status"=>200));