Skip to content

Commit fd7a3f4

Browse files
committed
Rewriting the autoload section to include composer
1 parent e3a9757 commit fd7a3f4

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

cookbook/symfony1.rst

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ That array told symfony1 exactly which file contained each class. In the
114114
production environment, this caused you to need to clear the cache when classes
115115
were added or moved.
116116

117-
In Symfony2, a new class - ``UniversalClassLoader`` - handles this process.
117+
In Symfony2, a tool named `Composer`_ handles this process.
118118
The idea behind the autoloader is simple: the name of your class (including
119119
the namespace) must match up with the path to the file containing that class.
120120
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.
136136
Specifically, the namespace, ``Sensio\Bundle\FrameworkExtraBundle``, spells out
137137
the directory that the file should live in
138138
(``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.
151140

152141
If the file did *not* live at this exact location, you'd receive a
153142
``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
160149
As mentioned before, for the autoloader to work, it needs to know that the
161150
``Sensio`` namespace lives in the ``vendor/bundles`` directory and that, for
162151
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.
165158

166159
If you look at the ``HelloController`` from the Symfony2 Standard Edition you
167160
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:
172164

173-
.. code-block:: php
174-
175-
// app/autoload.php
165+
.. code-block:: yaml
176166
177-
// ...
178-
$loader->registerNamespaceFallbacks(array(
179-
__DIR__.'/../src',
180-
));
167+
"autoload": {
168+
"psr-0": { "": "src/" }
169+
}
181170
182171
Using the Console
183172
-----------------
@@ -312,3 +301,4 @@ primarily to configure objects that you can use. For more information, see
312301
the chapter titled ":doc:`/book/service_container`".
313302

314303
.. _`Symfony2 Standard`: https://github.com/symfony/symfony-standard
304+
.. _`Composer`: http://getcomposer.org

0 commit comments

Comments
 (0)