0

我正在尝试使用 Silex 创建一个 RESTful 架构。我要实现的第一件事是节省一顿饭。我在帖子中路由“/餐”。出于调试目的,我向您展示获取请求而不是发布。这段代码不像我预期的那样工作。删除 executeUpdate 指令,一切正常。添加executeUpdate指令,网页返回一个空白页面。

<?php

require_once __DIR__ . '/../vendor/autoload.php';

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

$app = new Silex\Application();

$app['debug'] = true;

$app->register(
    new Silex\Provider\DoctrineServiceProvider(),
    array(
        'db' => array(
            'driver' => 'pdo_mysql',
            'host' => 'localhost',
            'dbname' => 'database',
            'user' => 'user',
            'password' => 'password',
            'charset' => 'utf8',
        ),
    )
);

$app->get(
    '/meals',
    function (Request $request) use ($app) {
        $app['db']->executeUpdate(
            'INSERT INTO meal_items (id, id_meal, food) VALUES (?, ?, ?)',
            array(
                null,
                1,
                'Onion'
            )
        );

        return new Response(json_encode(
            array(
                'hello'
            )
        ), 201);
    }
);

$app->run();
4

1 回答 1

1

You have to name it db.options instead of db in your array that you pass to the provider.

See here:

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => array(
        'driver' => 'pdo_mysql',
        'host' => 'localhost',
        'dbname' => 'database',
        'user' => 'user',
        'password' => 'password',
        'charset' => 'utf8',
    ),
));
于 2013-11-14T08:30:46.647 回答