diff --git a/book/internals.rst b/book/internals.rst index dd67e44e508..58f57d31dc2 100644 --- a/book/internals.rst +++ b/book/internals.rst @@ -23,7 +23,7 @@ on top of the previous one. Autoloading is not managed by the framework directly; it's done by using Composer's autoloader (``vendor/autoload.php``), which is included in - the ``src/autoload.php`` file. + the ``app/autoload.php`` file. ``HttpFoundation`` Component ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/book/page_creation.rst b/book/page_creation.rst index a2f74db1f9d..0f3d639a888 100644 --- a/book/page_creation.rst +++ b/book/page_creation.rst @@ -506,13 +506,13 @@ You'll learn more about each of these directories in later chapters. .. sidebar:: Autoloading - When Symfony is loading, a special file - ``app/autoload.php`` - is included. - This file is responsible for configuring the autoloader, which will autoload - your application files from the ``src/`` directory and third-party libraries - from the ``vendor/`` directory. + When Symfony is loading, a special file - ``vendor/autoload.php`` - is + included. This file is created by Composer and will autoload all + application files living in the `src/` folder as well as all + third-party libraries mentioned in the ``composer.json`` file. Because of the autoloader, you never need to worry about using ``include`` - or ``require`` statements. Instead, Symfony2 uses the namespace of a class + or ``require`` statements. Instead, Composer uses the namespace of a class to determine its location and automatically includes the file on your behalf the instant you need a class. @@ -527,11 +527,6 @@ You'll learn more about each of these directories in later chapters. Path: src/Acme/HelloBundle/Controller/HelloController.php - Typically, the only time you'll need to worry about the ``app/autoload.php`` - file is when you're including a new third-party library in the ``vendor/`` - directory. For more information on autoloading, see - :doc:`How to autoload Classes`. - The Source (``src``) Directory ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -603,7 +598,6 @@ are used by your application (including the core Symfony bundles). A bundle can live *anywhere* as long as it can be autoloaded (via the autoloader configured at ``app/autoload.php``). - Creating a Bundle ~~~~~~~~~~~~~~~~~ diff --git a/cookbook/symfony1.rst b/cookbook/symfony1.rst index 14ec51a0fca..4f35cd3f068 100644 --- a/cookbook/symfony1.rst +++ b/cookbook/symfony1.rst @@ -114,7 +114,7 @@ That array told symfony1 exactly which file contained each class. In the production environment, this caused you to need to clear the cache when classes were added or moved. -In Symfony2, a new class - ``UniversalClassLoader`` - handles this process. +In Symfony2, a tool named `Composer`_ handles this process. The idea behind the autoloader is simple: the name of your class (including the namespace) must match up with the path to the file containing that class. 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. Specifically, the namespace, ``Sensio\Bundle\FrameworkExtraBundle``, spells out the directory that the file should live in (``vendor/sensio/framework-extra-bundle/Sensio/Bundle/FrameworkExtraBundle/``). -This is because, in the ``app/autoload.php`` file, you'll configure Symfony to -look for the ``Sensio`` namespace in the ``vendor/sensio`` directory: - -.. code-block:: php - - // app/autoload.php - - // ... - $loader->registerNamespaces(array( - ..., - 'Sensio' => __DIR__.'/../vendor/sensio/framework-extra-bundle', - )); +Composer can then look for the file at this specific place and load it very fast. If the file did *not* live at this exact location, you'd receive a ``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 As mentioned before, for the autoloader to work, it needs to know that the ``Sensio`` namespace lives in the ``vendor/bundles`` directory and that, for example, the ``Doctrine`` namespace lives in the ``vendor/doctrine/orm/lib/`` -directory. This mapping is entirely controlled by you via the -``app/autoload.php`` file. +directory. This mapping is entirely controlled by Composer. Each +third-party library you load through composer has their settings defined +and Composer takes care of everything for you. + +For this to work, all third-party libraries used by your project must be +defined in the `composer.json` file. If you look at the ``HelloController`` from the Symfony2 Standard Edition you can see that it lives in the ``Acme\DemoBundle\Controller`` namespace. Yet, the -``Acme`` namespace is not defined in the ``app/autoload.php``. By default you -do not need to explicitly configure the location of bundles that live in the -``src/`` directory. The ``UniversalClassLoader`` is configured to fallback to -the ``src/`` directory using its ``registerNamespaceFallbacks`` method: +``AcmeDemoBundle`` is not defined in your `composer.json` file. Nonetheless are +the files autoloaded. This is because you can tell composer to autoload files +from specific directories without defining a dependency: -.. code-block:: php - - // app/autoload.php +.. code-block:: yaml - // ... - $loader->registerNamespaceFallbacks(array( - __DIR__.'/../src', - )); + "autoload": { + "psr-0": { "": "src/" } + } Using the Console ----------------- @@ -312,3 +301,4 @@ primarily to configure objects that you can use. For more information, see the chapter titled ":doc:`/book/service_container`". .. _`Symfony2 Standard`: https://github.com/symfony/symfony-standard +.. _`Composer`: http://getcomposer.org \ No newline at end of file