duanbei1709
2016-04-06 00:01
浏览 30
已采纳

使用Silex框架开发rest api

I'm starting to develop a rest api using silex. It's my first api!

What i've done:

Update: Code improved with the tip of Federic . Addition of Use Symfony JsonResponse. Not working for now.

<?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();

What it return?

{"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"}]}

How can i use JSON_PRETTY_PRINT to improve human read of my json object?

图片转代码服务由CSDN问答提供 功能建议

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

我做了什么:

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

 &lt;?php 
require_once __DIR __。'/ vendor / autoload.php'; 
 
use Symfony \ Component \ HttpFoundation \ JsonResponse; 
  
 // init Silex app 
 $ app = new Silex \ Application(); 
 $ app ['debug'] = true; 
 
 //配置数据库连接
 $ app-&gt; register(new  Silex \ Provider \ DoctrineServiceProvider(),array(
'db.options'=&gt; array(
'driver'=&gt;'pdo_mysql',
'host'=&gt;'127.0.0.1',
  'dbname'=&gt;'db',
'用户'=&gt;'root',
'密码'=&gt;'',
'charset'=&gt;'utf8',
),\  n)); 
 
 $ app-&gt; get('/ apps',function()use($ app){
 $ sql =“select apps.id,apps.guid,apps.title,clients。  apps.id_country = countries.id上的apps 
内部联接国家/地区的客户端,countries.country,langs.lang,apps.active客户端上的客户端.id = apps.id_client 
内部联接langs上的联接客户端 = apps.id_lang 
按apps.created_at desc排序“; 
 $ results = $ app ['db']  - &gt; fetchAll($ sql); 
 
 
 $ response ['status'] = array(
'code'=&gt;  200,
'message'=&gt;  '确定'
); 
 
 $ response ['data'] = $ results; 
 
 //返回$ app-&gt; json($ response); 
 $ jsonResponse = new JsonResponse($ response)  ); 
 $ jsonResponse-&gt; setEncodingOptions(JsonReponse :: DEFAULT_ENCODING_OPTIONS | JSON_PRETTY_PRINT); 
 
返回$ jsonResponse; 
}); 
 
 
 $ app-&gt; run(); 
 <  / code>  
 
 

它返回什么?

  {“status”:{“code”:200,“message”:“OK  “},”data“:[{”id“:”2“,”guid“:”a8559e9b-d850-4964-b672-335a87fe9e8b“,”title“:”Coverdine Plus Light“,”client“:”Servier“  “国”: “英格兰”, “郎”: “英语”, “积极”: “1”},{ “ID”: “1”, “GUID”: “4f242e9d-c041-4c79-bc82-b62604de403c”  “标题”: “Coverdine”, “客户端”: “施维雅”, “国”: “英国”, “朗”: “英语”, “活性”: “0”}]} 
  <  / pre> 
 
 

如何使用 JSON_PRETTY_PRINT 来改善人类对json对象的读取?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题