Skip to content

Commit 1489382

Browse files
committed
Merge branch '2.1'
2 parents a96b2ea + 52f1f65 commit 1489382

27 files changed

+250
-115
lines changed

book/controller.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,11 @@ headers and content that's sent back to the client::
705705
header names are normalized so that using ``Content-Type`` is equivalent
706706
to ``content-type`` or even ``content_type``.
707707

708+
.. tip::
709+
710+
There is also a special :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`
711+
class that helps return JSON responses. See :ref:`component-http-foundation-json-response`.
712+
708713
.. index::
709714
single: Controller; Request object
710715

book/internals.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ on top of the previous one.
2121

2222
.. tip::
2323

24-
Autoloading is not managed by the framework directly; it's done
25-
independently with the help of the
26-
:class:`Symfony\\Component\\ClassLoader\\UniversalClassLoader` class
27-
and the ``src/autoload.php`` file. Read the :doc:`dedicated chapter
28-
</components/class_loader>` for more information.
24+
Autoloading is not managed by the framework directly; it's done by using
25+
Composer's autoloader (``vendor/autoload.php``), which is included in
26+
the ``src/autoload.php`` file.
2927

3028
``HttpFoundation`` Component
3129
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

book/performance.rst

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,32 @@ your php.ini configuration.
4242
.. index::
4343
single: Performance; Autoloader
4444

45-
Use an Autoloader that caches (e.g. ``ApcUniversalClassLoader``)
46-
----------------------------------------------------------------
45+
Use Composer's Class Map Functionality
46+
--------------------------------------
4747

48-
By default, the Symfony2 standard edition uses the ``UniversalClassLoader``
49-
in the `autoloader.php`_ file. This autoloader is easy to use, as it will
48+
By default, the Symfony2 standard edition uses Composer's autoloader
49+
in the `autoload.php`_ file. This autoloader is easy to use, as it will
5050
automatically find any new classes that you've placed in the registered
5151
directories.
5252

5353
Unfortunately, this comes at a cost, as the loader iterates over all configured
5454
namespaces to find a particular file, making ``file_exists`` calls until it
5555
finally finds the file it's looking for.
5656

57-
The simplest solution is to cache the location of each class after it's located
57+
The simplest solution is to tell Composer to build a "class map" (i.e. a
58+
big array of the locations of all the classes). This can be done from the
59+
command line, and might become part of your deploy process::
60+
61+
.. code-block:: bash
62+
63+
php composer.phar dump-autoload --optimize
64+
65+
Internally, this builds the big class map array in ``vendor/composer/autoload_namespaces.php``.
66+
67+
Caching the Autoloader with APC
68+
-------------------------------
69+
70+
Another solution is to to cache the location of each class after it's located
5871
the first time. Symfony comes with a class - :class:`Symfony\\Component\\ClassLoader\\ApcClassLoader` -
5972
that does exactly this. To use it, just adapt your front controller file.
6073
If you're using the Standard Distribution, this code should already be available
@@ -125,5 +138,5 @@ is no longer a reason to use a bootstrap file.
125138

126139
.. _`byte code caches`: http://en.wikipedia.org/wiki/List_of_PHP_accelerators
127140
.. _`APC`: http://php.net/manual/en/book.apc.php
128-
.. _`autoloader.php`: https://github.com/symfony/symfony-standard/blob/master/app/autoload.php
141+
.. _`autoload.php`: https://github.com/symfony/symfony-standard/blob/master/app/autoload.php
129142
.. _`bootstrap file`: https://github.com/sensio/SensioDistributionBundle/blob/master/Composer/ScriptHandler.php

components/class_loader.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Installation
2828
You can install the component in many different ways:
2929

3030
* Use the official Git repository (https://github.com/symfony/ClassLoader);
31-
* Install it via Composer (``symfony/class-loader`` on `Packagist`_).
31+
* :doc:`Install it via Composer</components/using_components>` (``symfony/class-loader`` on `Packagist`_).
3232

3333
Usage
3434
-----

components/config/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Installation
1818
You can install the component in many different ways:
1919

2020
* Use the official Git repository (https://github.com/symfony/Config);
21-
* Install it via Composer (``symfony/config`` on `Packagist`_).
21+
* :doc:`Install it via Composer</components/using_components>` (``symfony/config`` on `Packagist`_).
2222

2323
Sections
2424
--------

components/console/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Installation
1818
You can install the component in many different ways:
1919

2020
* Use the official Git repository (https://github.com/symfony/Console);
21-
* Install it via Composer (``symfony/console`` on `Packagist`_).
21+
* :doc:`Install it via Composer</components/using_components>` (``symfony/console`` on `Packagist`_).
2222

2323
Creating a basic Command
2424
------------------------

components/css_selector.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Installation
1313
You can install the component in several different ways:
1414

1515
* Use the official Git repository (https://github.com/symfony/CssSelector);
16-
* Install it via Composer (``symfony/css-selector`` on `Packagist`_).
16+
* :doc:`Install it via Composer</components/using_components>` (``symfony/css-selector`` on `Packagist`_).
1717

1818
Usage
1919
-----

components/dependency_injection/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Installation
1717
You can install the component in many different ways:
1818

1919
* Use the official Git repository (https://github.com/symfony/DependencyInjection);
20-
* Install it via Composer (``symfony/dependency-injection`` on `Packagist`_).
20+
* :doc:`Install it via Composer</components/using_components>` (``symfony/dependency-injection`` on `Packagist`_).
2121

2222
Basic Usage
2323
-----------

components/dom_crawler.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Installation
1313
You can install the component in many different ways:
1414

1515
* Use the official Git repository (https://github.com/symfony/DomCrawler);
16-
* Install it via Composer (``symfony/dom-crawler`` on `Packagist`_).
16+
* :doc:`Install it via Composer</components/using_components>` (``symfony/dom-crawler`` on `Packagist`_).
1717

1818
Usage
1919
-----

components/event_dispatcher/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Installation
5050
You can install the component in many different ways:
5151

5252
* Use the official Git repository (https://github.com/symfony/EventDispatcher);
53-
* Install it via Composer (``symfony/event-dispatcher`` on `Packagist`_).
53+
* :doc:`Install it via Composer</components/using_components>` (``symfony/event-dispatcher`` on `Packagist`_).
5454

5555
Usage
5656
-----

components/finder.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Installation
1414
You can install the component in many different ways:
1515

1616
* Use the official Git repository (https://github.com/symfony/Finder);
17-
* Install it via Composer (``symfony/finder`` on `Packagist`_).
17+
* :doc:`Install it via Composer</components/using_components>` (``symfony/finder`` on `Packagist`_).
1818

1919
Usage
2020
-----

components/http_foundation/introduction.rst

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Installation
2222
You can install the component in many different ways:
2323

2424
* Use the official Git repository (https://github.com/symfony/HttpFoundation);
25-
* Install it via Composer (``symfony/http-foundation`` on `Packagist`_).
25+
* :doc:`Install it via Composer</components/using_components>` (``symfony/http-foundation`` on `Packagist`_).
2626

2727
Request
2828
-------
@@ -409,6 +409,49 @@ abstracts the hard work behind a simple API::
409409

410410
$response->headers->set('Content-Disposition', $d);
411411

412+
.. _component-http-foundation-json-response:
413+
414+
Creating a JSON Response
415+
~~~~~~~~~~~~~~~~~~~~~~~~
416+
417+
Any type of response can be created via the
418+
:class:`Symfony\\Component\\HttpFoundation\\Response` class by setting the
419+
right content and headers. A JSON response might look like this::
420+
421+
use Symfony\Component\HttpFoundation\Response;
422+
423+
$response = new Response();
424+
$response->setContent(json_encode(array(
425+
'data' => 123
426+
)));
427+
$response->headers->set('Content-Type', 'application/json');
428+
429+
.. versionadded:: 2.1
430+
The :class:`Symfony\\Component\\HttpFoundation\\JsonResponse` class was added in Symfony 2.1.
431+
432+
There is also a helpful :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`
433+
class, which can make this even easier::
434+
435+
use Symfony\Component\HttpFoundation\JsonResponse;
436+
437+
$response = new JsonResponse();
438+
$response->setContent(array(
439+
'data' => 123
440+
));
441+
442+
This encodes your array of data to JSON and sets the ``Content-Type`` header
443+
to ``application/json``. If you're using JSONP, you can set the callback
444+
function that the data should be passed to::
445+
446+
$response->setCallback('handleResponse');
447+
448+
In this case, the ``Content-Type`` header will be ``text/javascript`` and
449+
the response content will look like this:
450+
451+
.. code-block:: javascript
452+
453+
handleResponse({'data': 123});
454+
412455
Session
413456
-------
414457

components/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ The Components
44
.. toctree::
55
:hidden:
66

7+
using_components
78
class_loader
89
config/index
910
console/index

components/locale.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Installation
2828
You can install the component in many different ways:
2929

3030
* Use the official Git repository (https://github.com/symfony/Locale);
31-
* Install it via Composer (``symfony/locale`` on `Packagist`_).
31+
* :doc:`Install it via Composer</components/using_components>` (``symfony/locale`` on `Packagist`_).
3232

3333
Usage
3434
-----

components/map.rst.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* :doc:`/components/using_components`
2+
13
* **Class Loader**
24

35
* :doc:`/components/class_loader`

components/process.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Installation
1313
You can install the component in many different ways:
1414

1515
* Use the official Git repository (https://github.com/symfony/Process);
16-
* Install it via Composer (``symfony/process`` on `Packagist`_).
16+
* :doc:`Install it via Composer</components/using_components>` (``symfony/process`` on `Packagist`_).
1717

1818
Usage
1919
-----

components/routing/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Installation
1414
You can install the component in many different ways:
1515

1616
* Use the official Git repository (https://github.com/symfony/Routing);
17-
* Install it via Composer (``symfony/routing`` on `Packagist`_).
17+
* :doc:`Install it via Composer</components/using_components>` (``symfony/routing`` on `Packagist`_).
1818

1919
Usage
2020
-----

components/serializer.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Installation
2828
You can install the component in many different ways:
2929

3030
* Use the official Git repository (https://github.com/symfony/Serializer);
31-
* Install it via Composer (``symfony/serializer`` on `Packagist`_).
31+
* :doc:`Install it via Composer</components/using_components>` (``symfony/serializer`` on `Packagist`_).
3232

3333
Usage
3434
-----

components/templating.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Installation
1919
You can install the component in many different ways:
2020

2121
* Use the official Git repository (https://github.com/symfony/Templating);
22-
* Install it via Composer (``symfony/templating`` on `Packagist`_).
22+
* :doc:`Install it via Composer</components/using_components>` (``symfony/templating`` on `Packagist`_).
2323

2424
Usage
2525
-----

components/using_components.rst

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
.. index::
2+
single: Components; Installation
3+
single: Components; Usage
4+
5+
How to Install and Use the Symfony2 Components
6+
==============================================
7+
8+
If you're starting a new project (or already have a project) that will use
9+
one or more components, the easiest way to integrate everything is with Composer.
10+
Composer is smart enough to download the component(s) that you need and take
11+
care of autoloading so that you can begin using the libraries immediately.
12+
13+
This article will take you through using the :doc:`/components/finder`, though
14+
this applies to using any component.
15+
16+
Using the Finder Component
17+
--------------------------
18+
19+
**1.** If you're creating a new project, create a new empty directory for it.
20+
21+
**2.** Create a new file called ``composer.json`` and paste the following into it:
22+
23+
.. code-block:: json
24+
25+
{
26+
"require": {
27+
"symfony/finder": "2.1.*"
28+
}
29+
}
30+
31+
If you already have a ``composer.json`` file, just add this line to it. You
32+
may also need to adjust the version (e.g. ``2.1.1`` or ``2.2.*``).
33+
34+
You can research the component names and versions at `packagist.org`_.
35+
36+
**3.** Download the vendor libraries and generate the ``vendor/autoload.php`` file:
37+
38+
.. code-block:: bash
39+
40+
$ php composer.phar install
41+
42+
**4.** Write your code:
43+
44+
Once Composer has downloaded the component(s), all you need to do is include
45+
the ``vendor/autoload.php`` file that was generated by Composer. This file
46+
takes care of autoloading all of the libraries so that you can use them
47+
immediately::
48+
49+
// File: src/script.php
50+
51+
require_once '../vendor/autoload.php';
52+
53+
use Symfony\Component\Finder\Finder;
54+
55+
$finder = new Finder();
56+
$finder->in('../data/');
57+
58+
// ...
59+
60+
.. tip::
61+
62+
If you want to use all of the Symfony2 Components, then instead of adding
63+
them one by one:
64+
65+
.. code-block:: json
66+
67+
{
68+
"require": {
69+
"symfony/finder": "2.1.*",
70+
"symfony/dom-crawler": "2.1.*",
71+
"symfony/css-selector": "2.1.*"
72+
}
73+
}
74+
75+
you can use:
76+
77+
.. code-block:: json
78+
79+
{
80+
"require": {
81+
"symfony/symfony": "2.1.*"
82+
}
83+
}
84+
85+
This will include the Bundle and Bridge libraries, which you may not
86+
actually need.
87+
88+
Now What?
89+
---------
90+
91+
Now that the component is installed and autoloaded, read the specific component's
92+
documentation to find out more about how to use it.
93+
94+
And have fun!
95+
96+
.. _packagist.org: https://packagist.org/

components/yaml.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Installation
2727
You can install the component in many different ways:
2828

2929
* Use the official Git repository (https://github.com/symfony/Yaml);
30-
* Install it via Composer (``symfony/yaml`` on `Packagist`_).
30+
* :doc:`Install it via Composer</components/using_components>` (``symfony/yaml`` on `Packagist`_).
3131

3232
Why?
3333
----

cookbook/debugging.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ below::
4646
// ...
4747

4848
// require_once __DIR__.'/../app/bootstrap.php.cache';
49-
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
5049
require_once __DIR__.'/../app/autoload.php';
5150
require_once __DIR__.'/../app/AppKernel.php';
5251

0 commit comments

Comments
 (0)