Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eae3a57

Browse files
committed
Merge pull request symfony#2867 from WouterJ/issue_2331
Unified autoloading in Sf2 <> Sf1 article
2 parents 5e27db9 + 9e4b6d0 commit eae3a57

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

cookbook/symfony1.rst

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ were added or moved.
119119
In Symfony2, a tool named `Composer`_ handles this process.
120120
The idea behind the autoloader is simple: the name of your class (including
121121
the namespace) must match up with the path to the file containing that class.
122-
Take the ``FrameworkExtraBundle`` from the Symfony2 Standard Edition as an
122+
Take the FrameworkExtraBundle from the Symfony2 Standard Edition as an
123123
example::
124124

125125
namespace Sensio\Bundle\FrameworkExtraBundle;
@@ -134,34 +134,38 @@ example::
134134

135135
The file itself lives at
136136
``vendor/sensio/framework-extra-bundle/Sensio/Bundle/FrameworkExtraBundle/SensioFrameworkExtraBundle.php``.
137-
As you can see, the location of the file follows the namespace of the class.
138-
Specifically, the namespace, ``Sensio\Bundle\FrameworkExtraBundle``, spells out
139-
the directory that the file should live in
137+
As you can see, the second part of the path follows the namespace of the
138+
class. The first part is equal to the package name of the SensioFrameworkExtraBundle.
139+
140+
The namespace, ``Sensio\Bundle\FrameworkExtraBundle``, and package name,
141+
``sensio/framework-extra-bundle``, spells out the directory that the file
142+
should live in
140143
(``vendor/sensio/framework-extra-bundle/Sensio/Bundle/FrameworkExtraBundle/``).
141-
Composer can then look for the file at this specific place and load it very fast.
144+
Composer can then look for the file at this specific place and load it very
145+
fast.
142146

143147
If the file did *not* live at this exact location, you'd receive a
144148
``Class "Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle" does not exist.``
145-
error. In Symfony2, a "class does not exist" means that the suspect class
146-
namespace and physical location do not match. Basically, Symfony2 is looking
149+
error. In Symfony2, a "class does not exist" error means that the namespace of
150+
the class and physical location do not match. Basically, Symfony2 is looking
147151
in one exact location for that class, but that location doesn't exist (or
148152
contains a different class). In order for a class to be autoloaded, you
149153
**never need to clear your cache** in Symfony2.
150154

151155
As mentioned before, for the autoloader to work, it needs to know that the
152-
``Sensio`` namespace lives in the ``vendor/bundles`` directory and that, for
153-
example, the ``Doctrine`` namespace lives in the ``vendor/doctrine/orm/lib/``
154-
directory. This mapping is entirely controlled by Composer. Each
155-
third-party library you load through composer has their settings defined
156-
and Composer takes care of everything for you.
156+
``Sensio`` namespace lives in the ``vendor/sensio/framework-extra-bundle``
157+
directory and that, for example, the ``Doctrine`` namespace lives in the
158+
``vendor/doctrine/orm/lib/`` directory. This mapping is entirely controlled by
159+
Composer. Each third-party library you load through Composer has their
160+
settings defined and Composer takes care of everything for you.
157161

158162
For this to work, all third-party libraries used by your project must be
159163
defined in the ``composer.json`` file.
160164

161165
If you look at the ``HelloController`` from the Symfony2 Standard Edition you
162166
can see that it lives in the ``Acme\DemoBundle\Controller`` namespace. Yet, the
163167
``AcmeDemoBundle`` is not defined in your ``composer.json`` file. Nonetheless are
164-
the files autoloaded. This is because you can tell composer to autoload files
168+
the files autoloaded. This is because you can tell Composer to autoload files
165169
from specific directories without defining a dependency:
166170

167171
.. code-block:: yaml
@@ -170,6 +174,11 @@ from specific directories without defining a dependency:
170174
"psr-0": { "": "src/" }
171175
}
172176
177+
This means that if a class is not found in the ``vendor`` directory, Composer
178+
will search in the ``src`` directory before throwing a "class does not exists"
179+
exception. Read more about configuring the Composer Autoloader in
180+
`the Composer documentation`_
181+
173182
Using the Console
174183
-----------------
175184

@@ -357,3 +366,4 @@ the chapter titled ":doc:`/book/service_container`".
357366

358367
.. _`Composer`: http://getcomposer.org
359368
.. _`Symfony2 Standard Edition`: https://github.com/symfony/symfony-standard
369+
.. _`the Composer documentation`: http://getcomposer.org/doc/04-schema.md#autoload

0 commit comments

Comments
 (0)