【问题标题】:Develop a rest api using Silex framework使用Silex框架开发一个rest api
【发布时间】:2016-04-06 00:01:04
【问题描述】:

我开始使用 silex 开发一个 rest api。这是我的第一个 API!

我做了什么:

更新: 使用 Federic 的提示改进了代码。添加使用 Symfony JsonResponse。暂时不工作。

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

use Symfony\Component\HttpFoundation\JsonResponse;

// init Silex app
$app = new Silex\Application();
$app['debug'] = true;

//configure database connection
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => array(
        'driver' => 'pdo_mysql',
        'host' => '127.0.0.1',
        'dbname' => 'db',
        'user' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ),
));

$app->get('/apps', function () use ($app){
    $sql = "select apps.id, apps.guid, apps.title, clients.client, countries.country, langs.lang, apps.active from apps
            inner join countries on apps.id_country = countries.id
            inner join clients on clients.id = apps.id_client
            inner join langs on langs.id = apps.id_lang
            order by apps.created_at desc";
    $results = $app['db']->fetchAll($sql);


        $response['status'] = array(
            'code' => 200,
            'message' => 'OK'
        );

        $response['data'] = $results;

    //return $app->json($response);
    $jsonResponse = new JsonResponse($response);
    $jsonResponse->setEncodingOptions(JsonReponse::DEFAULT_ENCODING_OPTIONS | JSON_PRETTY_PRINT);

    return $jsonResponse;
});


$app->run();

它返回什么?

{"status":{"code":200,"message":"OK"},"data":[{"id":"2","guid":"a8559e9b-d850-4964-b672-335a87fe9e8b","title":"Coverdine Plus Light","client":"Servier","country":"England","lang":"English","active":"1"},{"id":"1","guid":"4f242e9d-c041-4c79-bc82-b62604de403c","title":"Coverdine","client":"Servier","country":"England","lang":"English","active":"0"}]}

我如何使用JSON_PRETTY_PRINT 来提高对我的 json 对象的人工读取?

【问题讨论】:

  • 您应该使用 Postman 之类的工具来测试您的 api,而不是漂亮地打印您的响应,它会为您格式化响应以提高可读性。
  • Max P. - 感谢您的链接。它对我有用。

标签: php json rest silex


【解决方案1】:

不要使用$app-&gt;json($response);,而是使用JsonResponse。您可以使用他的setEncodingOptions 方法设置JSON_PRETTY_PRINT 标志。

<?php
use Symfony\Component\HttpFoundation\JsonResponse;

// ...
$jsonResponse = new JsonResponse($response);
$jsonResponse->setEncodingOptions(JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_PRETTY_PRINT);

return $jsonResponse;

【讨论】:

  • 我已经尝试过这个解决方案,但它给了我这个错误:PHP致命错误:在中找不到类'JsonReponse'
  • 你错过了use Symfony\Component\HttpFoundation\JsonResponse;吗?
【解决方案2】:

这对我有用:

...
require_once(__DIR__ .'/../vendor/autoload.php');
use Symfony\Component\HttpFoundation\Response;

...
$response = $sth->fetchALL(PDO::FETCH_ASSOC);
return $app->json($response, Response::HTTP_OK)->setEncodingOptions(JSON_PRETTY_PRINT);

【讨论】:

    【解决方案3】:

    我也遇到过这个困难,研究了很久才找到这个函数:do_dump,click here可以在github上看到。

    要使用它,只需这样做:

    
    
    $var = json_decode('{"a":1,"b":2,"c":3,"d":4,"e":5}');
    do_dump($var);
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2020-05-15
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      相关资源
      最近更新 更多