This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpFoundation
Fabien Potencier 0e5ac97ee2 feature #17642 [FrameworkBundle] [DX] Add Controller::json method to make it easy to send json (mcfedr)
This PR was merged into the 3.1-dev branch.

Discussion
----------

[FrameworkBundle] [DX] Add `Controller::json` method to make it easy to send json

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Its currently a awkward to use Serializer component to send a `JsonResponse`.

I have tried two approaches

1. use `Serializer::normalize` and `JsonResponse`
1. use `Serializer::serialize` and a plain `Response`, and set the `content-type`

In either cases there is need for a custom `json` function so as not to repeat yourself and there are disadvantages.

1. In the first case you are only partly using `Serializer` and any custom `Encoder` would be skipped
1. In the second you are not making use of `JsonResponse`, particular disadvantage if you want to support JSONP.

This new `json` method uses the serializer component is enabled it is used to generate the json data, and falls back to normal `JsonResponse` when its not.

Commits
-------

f904a2b Add a Controller function to make it easy to return json
2016-03-03 14:18:37 +01:00
..
File Merge branch '2.3' into 2.7 2015-09-29 14:06:14 +02:00
Session [HttpFoundation] [Session] Removed unnecessary PHP version check as minimum requirement is now 5.5.9 2016-02-15 18:23:19 +02:00
Tests feature #17642 [FrameworkBundle] [DX] Add Controller::json method to make it easy to send json (mcfedr) 2016-03-03 14:18:37 +01:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
AcceptHeader.php [2.3] CS And DocBlock Fixes 2014-12-22 16:58:09 +01:00
AcceptHeaderItem.php fixed types in phpdocs 2014-04-16 12:30:19 +02:00
ApacheRequest.php [HttpFoundation] do not use server variable PATH_INFO because it is already decoded and thus symfony is fragile to double encoding of the path 2013-04-12 17:03:10 +02:00
BinaryFileResponse.php Merge branch '2.3' into 2.7 2016-02-28 17:19:47 +01:00
CHANGELOG.md Add a Controller function to make it easy to return json 2016-03-02 16:31:41 +02:00
composer.json add missing symfony/polyfill-php55 dependency 2015-12-28 15:56:15 +01:00
Cookie.php [HttpFoundation][Cookie] Cookie DateTimeInterface fix 2016-01-14 17:21:08 +01:00
ExpressionRequestMatcher.php [HttpFoundation] added ExpressionRequestMatcher 2013-09-19 12:59:11 +02:00
FileBag.php remove api tags from code 2015-09-28 19:11:22 +02:00
HeaderBag.php Merge branch '2.3' into 2.7 2016-01-12 18:44:11 +01:00
IpUtils.php [HttpFoundation] Fixes /0 subnet handling in IpUtils 2015-10-19 13:54:29 +02:00
JsonResponse.php Add a Controller function to make it easy to return json 2016-03-02 16:31:41 +02:00
LICENSE Update copyright year 2016-01-01 23:53:47 -03:00
ParameterBag.php [HttpFoundation] Remove @throws from ParameterBag::get() PHPDoc. This was for the now removed deep flag. 2016-02-24 12:32:18 -06:00
phpunit.xml.dist [PhpUnit] Auto-register SymfonyTestsListener 2015-10-11 10:29:26 +02:00
README.md Rely on iconv and symfony/polyfill-* 2015-10-28 03:15:07 +01:00
RedirectResponse.php documented the $url parameter better 2016-02-18 14:55:48 +01:00
Request.php Merge branch '3.0' 2016-02-04 13:57:09 +01:00
RequestMatcher.php Merge branch '2.3' into 2.7 2015-09-29 14:06:14 +02:00
RequestMatcherInterface.php remove api tags from code 2015-09-28 19:11:22 +02:00
RequestStack.php unified return null usages 2014-04-18 22:38:54 +02:00
Response.php Merge branch '2.8' into 3.0 2016-03-02 11:18:25 +01:00
ResponseHeaderBag.php Merge branch '2.3' into 2.7 2016-01-12 18:44:11 +01:00
ServerBag.php [HttpFoundation] Do not overwrite the Authorization header if it is already set 2016-01-22 07:46:45 +01:00
StreamedResponse.php Add more callable type hints 2015-10-05 18:52:37 +02:00

HttpFoundation Component

HttpFoundation defines an object-oriented layer for the HTTP specification.

It provides an abstraction for requests, responses, uploaded files, cookies, sessions, ...

In this example, we get a Request object from the current PHP global variables:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$request = Request::createFromGlobals();
echo $request->getPathInfo();

You can also create a Request directly -- that's interesting for unit testing:

$request = Request::create('/?foo=bar', 'GET');
echo $request->getPathInfo();

And here is how to create and send a Response:

$response = new Response('Not Found', 404, array('Content-Type' => 'text/plain'));
$response->send();

The Request and the Response classes have many other methods that implement the HTTP specification.

Resources

You can run the unit tests with the following command:

$ cd path/to/Symfony/Component/HttpFoundation/
$ composer install
$ phpunit