@@ -114,7 +114,7 @@ That array told symfony1 exactly which file contained each class. In the
114
114
production environment, this caused you to need to clear the cache when classes
115
115
were added or moved.
116
116
117
- In Symfony2, a new class - `` UniversalClassLoader `` - handles this process.
117
+ In Symfony2, a tool named ` Composer `_ handles this process.
118
118
The idea behind the autoloader is simple: the name of your class (including
119
119
the namespace) must match up with the path to the file containing that class.
120
120
Take the ``FrameworkExtraBundle `` from the Symfony2 Standard Edition as an
@@ -136,18 +136,7 @@ As you can see, the location of the file follows the namespace of the class.
136
136
Specifically, the namespace, ``Sensio\Bundle\FrameworkExtraBundle ``, spells out
137
137
the directory that the file should live in
138
138
(``vendor/sensio/framework-extra-bundle/Sensio/Bundle/FrameworkExtraBundle/ ``).
139
- This is because, in the ``app/autoload.php `` file, you'll configure Symfony to
140
- look for the ``Sensio `` namespace in the ``vendor/sensio `` directory:
141
-
142
- .. code-block :: php
143
-
144
- // app/autoload.php
145
-
146
- // ...
147
- $loader->registerNamespaces(array(
148
- ...,
149
- 'Sensio' => __DIR__.'/../vendor/sensio/framework-extra-bundle',
150
- ));
139
+ Composer can then look for the file at this specific place and load it very fast.
151
140
152
141
If the file did *not * live at this exact location, you'd receive a
153
142
``Class "Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle" does not exist. ``
@@ -160,24 +149,24 @@ contains a different class). In order for a class to be autoloaded, you
160
149
As mentioned before, for the autoloader to work, it needs to know that the
161
150
``Sensio `` namespace lives in the ``vendor/bundles `` directory and that, for
162
151
example, the ``Doctrine `` namespace lives in the ``vendor/doctrine/orm/lib/ ``
163
- directory. This mapping is entirely controlled by you via the
164
- ``app/autoload.php `` file.
152
+ directory. This mapping is entirely controlled by Composer. Each
153
+ third-party library you load through composer has their settings defined
154
+ and Composer takes care of everything for you.
155
+
156
+ For this to work, all third-party libraries used by your project must be
157
+ defined in the `composer.json ` file.
165
158
166
159
If you look at the ``HelloController `` from the Symfony2 Standard Edition you
167
160
can see that it lives in the ``Acme\DemoBundle\Controller `` namespace. Yet, the
168
- ``Acme `` namespace is not defined in the ``app/autoload.php ``. By default you
169
- do not need to explicitly configure the location of bundles that live in the
170
- ``src/ `` directory. The ``UniversalClassLoader `` is configured to fallback to
171
- the ``src/ `` directory using its ``registerNamespaceFallbacks `` method:
161
+ ``AcmeDemoBundle `` is not defined in your `composer.json ` file. Nonetheless are
162
+ the files autoloaded. This is because you can tell composer to autoload files
163
+ from specific directories without defining a dependency:
172
164
173
- .. code-block :: php
174
-
175
- // app/autoload.php
165
+ .. code-block :: yaml
176
166
177
- // ...
178
- $loader->registerNamespaceFallbacks(array(
179
- __DIR__.'/../src',
180
- ));
167
+ " autoload " : {
168
+ " psr-0 " : { "": "src/" }
169
+ }
181
170
182
171
Using the Console
183
172
-----------------
@@ -312,3 +301,4 @@ primarily to configure objects that you can use. For more information, see
312
301
the chapter titled ":doc: `/book/service_container `".
313
302
314
303
.. _`Symfony2 Standard` : https://github.com/symfony/symfony-standard
304
+ .. _`Composer` : http://getcomposer.org
0 commit comments