Backbone.js 和带有 Silex 的 REST API (PHP)

发布于 2024-12-25 01:14:51 字数 1000 浏览 0 评论 0原文

可以说我有一个名为 John 的模型,其中包含这些参数:

{
    Language : {
        code    :  'gr',
        title   :  'Greek'
    },
    Name : 'john'
}

所以现在当我触发 John.save() 时,它会将这些参数 POST 到服务器:

post params http://o7.no/ypvWNp

以及这些标头:

标头 http://o7.no/x5DVw0

代码在 Silex 中非常简单:

<?php

require_once __DIR__.'/silex.phar';

$app = new Silex\Application();

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

// definitions
$app['debug'] = true;

$app->post('/api/user', function (Request $request) {
    var_dump($request->get('Name'));

    $params = json_decode(file_get_contents('php://input'));
    var_dump($params->Name);
});

$app->run();

但是第一个 var_dump 返回 null 第二个 var_dump 当然可以工作,因为我直接从 php://input 资源获取请求。我想知道如何使用 Silex 的 Request 对象获取参数

谢谢

lets say I have a model called John with those params:

{
    Language : {
        code    :  'gr',
        title   :  'Greek'
    },
    Name : 'john'
}

So now when I trigger John.save() it POST those to server:

post params http://o7.no/ypvWNp

with those headers:

headers http://o7.no/x5DVw0

The code in Silex is really simple:

<?php

require_once __DIR__.'/silex.phar';

$app = new Silex\Application();

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

// definitions
$app['debug'] = true;

$app->post('/api/user', function (Request $request) {
    var_dump($request->get('Name'));

    $params = json_decode(file_get_contents('php://input'));
    var_dump($params->Name);
});

$app->run();

but first var_dump return null second var_dump of course works since I'm getting the request directly from php://input resource. I'm wondering how I could get the params using Request object from Silex

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烛影斜 2025-01-01 01:14:52

实际上这很容易。

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;

$app->before(function (Request $request) {
    if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
        $data = json_decode($request->getContent(), true);
        $request->request = new ParameterBag(is_array($data) ? $data : array());
    }
});

然后是一个示例路线:

$app->match('/', function (Request $request) {
    return $request->get('foo');
});

并使用curl进行测试:

$ curl http://localhost/foobarbazapp/app.php -d '{"foo": "bar"}' -H 'Content-Type: application/json'
bar
$

或者查看(稍微过时的)RestServiceProvider

编辑:我已将这个答案变成 silex 文档中的食谱

It's pretty easy actually.

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;

$app->before(function (Request $request) {
    if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
        $data = json_decode($request->getContent(), true);
        $request->request = new ParameterBag(is_array($data) ? $data : array());
    }
});

And then an example route:

$app->match('/', function (Request $request) {
    return $request->get('foo');
});

And testing with curl:

$ curl http://localhost/foobarbazapp/app.php -d '{"foo": "bar"}' -H 'Content-Type: application/json'
bar
$

Alternatively look at the (slightly outdated) RestServiceProvider.

EDIT: I have turned this answer into a cookbook recipe in the silex documentation.

琉璃繁缕 2025-01-01 01:14:52

我之前的做法如下:

$app->post('/api/todos', function (Request $request) use ($app) {
    $data = json_decode($request->getContent());

    $todo =  $app['paris']->getModel('Todo')->create();
    $todo->title = $data->title;
    $todo->save();

    return new Response(json_encode($todo->as_array()), 200, array('Content-Type' => 'application/json'));
});

在您的主干集合中,添加以下内容:

window.TodoList = Backbone.Collection.extend({
    model: Todo,

    url: "api/todos",

    ...
});

我在这里编写了完整的分步教程 http://cambridgesoftware.co.uk/blog/item/59-backbonejs-%20-php-with-silex-microframework-%20-mysql

The way I have done it before is the following:

$app->post('/api/todos', function (Request $request) use ($app) {
    $data = json_decode($request->getContent());

    $todo =  $app['paris']->getModel('Todo')->create();
    $todo->title = $data->title;
    $todo->save();

    return new Response(json_encode($todo->as_array()), 200, array('Content-Type' => 'application/json'));
});

In your backbone collection, add the following:

window.TodoList = Backbone.Collection.extend({
    model: Todo,

    url: "api/todos",

    ...
});

I have written up a full step-by-step tutorial here http://cambridgesoftware.co.uk/blog/item/59-backbonejs-%20-php-with-silex-microframework-%20-mysql

亣腦蒛氧 2025-01-01 01:14:52

我自己通过在 Request 对象上设置额外的 $payload 属性解决了这个问题

$app->before(function(Request $request) {
    if (stristr($request->getContentType(), 'json')) {
        $data             = json_decode($request->getContent());
        $request->payload = $data;
    } else {
        $request->payload = null;
    }
});

I've solved it myself by setting an extra $payload property on the Request object

$app->before(function(Request $request) {
    if (stristr($request->getContentType(), 'json')) {
        $data             = json_decode($request->getContent());
        $request->payload = $data;
    } else {
        $request->payload = null;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文