Skip to content

Changing description of autoloading to "The composer way" #1996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still true (except that src/autoload.php doesn't exists, it should be app/autoload.php)


``HttpFoundation`` Component
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
16 changes: 5 additions & 11 deletions book/page_creation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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</components/class_loader>`.

The Source (``src``) Directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -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
~~~~~~~~~~~~~~~~~

Expand Down
42 changes: 16 additions & 26 deletions cookbook/symfony1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.``
Expand All @@ -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
-----------------
Expand Down Expand Up @@ -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