2016-04-06 23 views
1

Silex kullanarak bir api geliştirmeye başlıyorum. Bu benim ilk api!Silex framework'ü kullanarak bir dinlenme api geliştirin

i yaptık Ne:

Güncelleme: Kod Federic ucuyla düzeldi. Kullanım Symfony JsonResponse eklenmesi. Şimdilik çalışmıyor.

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

Neler döndü?

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

Nasıl benim json nesnenin insan okuma geliştirmek için JSON_PRETTY_PRINT kullanabilir ?

+0

yerine yanıtınızı prettyPrinting, kendi api test etmek için Postman gibi bir araç kullanmalıdır, bu biçimlendirir okunabilirlik için yanıt. – Maerlyn

+0

http://stackoverflow.com/questions/35142601/silex-app-json-returning-integer-data-as-strings/35161207#35161207 –

+0

Max P. - Bağlantı için teşekkürler. Benim için çalıştı. –

cevap

3

$app->json($response); kullanmak yerine, JsonResponse kullanın. setEncodingOptions yöntemiyle JSON_PRETTY_PRINT bayrağını ayarlayabilirsiniz.

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

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

return $jsonResponse; 
+0

Bu çözümü denedim ama bana bu hatayı veriyor: PHP Önemli hata: –

+0

sınıfında bulunamadı 'JsonReponse' sınıfı 'Symfony \ Component \ HttpFoundation \ JsonResponse; – Federkun

0

Ben de bu güçlükleri olmuş ve çok araştırmadan sonra ben fonksiyonu bulundu: do_dump, click here github olarak görmek.

Kullanmak için, sadece bu yapın:



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

2

bu benim için çalıştı:

... 
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); 
İlgili konular