Commit b35806a7 authored by Fabien Potencier's avatar Fabien Potencier

merged branch igorw/json (PR #285)

Commits
-------

aee65e34 [json] add docs to the usage page
b2566895 [json] adjust json request body recipe
4ce8998a [json] add JSON helper

Discussion
----------

Json
parents f781d911 aee65e34
...@@ -84,8 +84,7 @@ return the post object, including its ``id``, as JSON. ...@@ -84,8 +84,7 @@ return the post object, including its ``id``, as JSON.
$post['id'] = createPost($post); $post['id'] = createPost($post);
$json = json_encode($post); return $app->json($post, 201);
return new Response($json, 201, array('Content-Type' => 'application/json'));
}); });
Manual testing Manual testing
......
...@@ -523,6 +523,26 @@ you can create by calling the ``redirect`` method:: ...@@ -523,6 +523,26 @@ you can create by calling the ``redirect`` method::
This will redirect from ``/`` to ``/hello``. This will redirect from ``/`` to ``/hello``.
JSON
----
If you want to return JSON data, you can use the ``json`` helper method.
Simply pass it your data, status code and headers, and it will create a
JSON response for you.
.. code-block:: php
$app->get('/users/{id}', function ($id) use ($app) {
$user = getUser($id);
if (!$user) {
$error = array('message' => 'The user was not found.');
return $app->json($error, 404);
}
return $app->json($user);
});
Streaming Streaming
--------- ---------
......
...@@ -30,6 +30,7 @@ use Symfony\Component\HttpFoundation\Request; ...@@ -30,6 +30,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
...@@ -348,6 +349,20 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -348,6 +349,20 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
return htmlspecialchars($text, $flags, $charset, $doubleEncode); return htmlspecialchars($text, $flags, $charset, $doubleEncode);
} }
/**
* Convert some data into a JSON response.
*
* @param mixed $data The response data
* @param integer $status The response status code
* @param array $headers An array of response headers
*
* @see Symfony\Component\HttpFoundation\JsonResponse
*/
public function json($data = array(), $status = 200, $headers = array())
{
return new JsonResponse($data, $status, $headers);
}
/** /**
* Mounts an application under the given route prefix. * Mounts an application under the given route prefix.
* *
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* JSON test cases.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class JsonTest extends \PHPUnit_Framework_TestCase
{
public function testJsonReturnsJsonResponse()
{
$app = new Application();
$response = $app->json();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('{}', $response->getContent());
}
public function testJsonUsesData()
{
$app = new Application();
$response = $app->json(array('foo' => 'bar'));
$this->assertSame('{"foo":"bar"}', $response->getContent());
}
public function testJsonUsesStatus()
{
$app = new Application();
$response = $app->json(array(), 202);
$this->assertSame(202, $response->getStatusCode());
}
public function testJsonUsesHeaders()
{
$app = new Application();
$response = $app->json(array(), 200, array('ETag' => 'foo'));
$this->assertSame('foo', $response->headers->get('ETag'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment