Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

merged branch igorw/json (PR #285)

Commits
-------

aee65e3 [json] add docs to the usage page
b256689 [json] adjust json request body recipe
4ce8998 [json] add JSON helper

Discussion
----------

Json
  • Loading branch information...
commit b35806a72e0d9e3ee902fd44d651c6259d806b9a 2 parents f781d91 + aee65e3
@fabpot authored
View
3  doc/cookbook/json_request_body.rst
@@ -84,8 +84,7 @@ return the post object, including its ``id``, as JSON.
$post['id'] = createPost($post);
- $json = json_encode($post);
- return new Response($json, 201, array('Content-Type' => 'application/json'));
+ return $app->json($post, 201);
});
Manual testing
View
20 doc/usage.rst
@@ -523,6 +523,26 @@ you can create by calling the ``redirect`` method::
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
---------
View
15 src/Silex/Application.php
@@ -30,6 +30,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
+use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -349,6 +350,20 @@ public function escape($text, $flags = ENT_COMPAT, $charset = 'UTF-8', $doubleEn
}
/**
+ * 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.
*
* @param string $prefix The route prefix
View
58 tests/Silex/Tests/JsonTest.php
@@ -0,0 +1,58 @@
+<?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'));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.