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
3 changes: 1 addition & 2 deletions book/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ on top of the previous one.
.. tip::

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.
Composer's autoloader (``vendor/autoload.php``).

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
20 changes: 5 additions & 15 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 @@ -599,11 +594,6 @@ method of the ``AppKernel`` class::
With the ``registerBundles()`` method, you have total control over which bundles
are used by your application (including the core Symfony bundles).

.. tip::

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
3 changes: 1 addition & 2 deletions book/performance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Use Composer's Class Map Functionality
--------------------------------------

By default, the Symfony2 standard edition uses Composer's autoloader
in the `autoload.php`_ file. This autoloader is easy to use, as it will
in the `vendor/autoload.php` file. This autoloader is easy to use, as it will
Copy link
Member

Choose a reason for hiding this comment

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

you should keep the existing link here, as it is the link to the place where the SE uses the composer autoloader

automatically find any new classes that you've placed in the registered
directories.

Expand Down Expand Up @@ -138,5 +138,4 @@ is no longer a reason to use a bootstrap file.

.. _`byte code caches`: http://en.wikipedia.org/wiki/List_of_PHP_accelerators
.. _`APC`: http://php.net/manual/en/book.apc.php
.. _`autoload.php`: https://github.com/symfony/symfony-standard/blob/master/app/autoload.php
.. _`bootstrap file`: https://github.com/sensio/SensioDistributionBundle/blob/master/Composer/ScriptHandler.php
3 changes: 1 addition & 2 deletions contributing/code/tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ Dependencies (optional)

To run the entire test suite, including tests that depend on external
dependencies, Symfony2 needs to be able to autoload them. By default, they are
autoloaded from `vendor/` under the main root directory (see
`autoload.php.dist`).
autoloaded from `vendor/` under the main root directory.

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 (see vendor/symfony/symfony/autoload.php.dist, which includes the Composer autoloader)

The test suite needs the following third-party libraries:

Expand Down
1 change: 0 additions & 1 deletion cookbook/debugging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ below::
// ...

// require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/autoload.php';
require_once __DIR__.'/../app/AppKernel.php';
Copy link
Member

Choose a reason for hiding this comment

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

Removing this is wrong. The autoloader won't work if it is not loaded

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you sure? Neither my projects nor the symfony standard edition loads it from 2.1 on:
https://github.com/symfony/symfony-standard/blob/2.1/web/app_dev.php

Copy link
Member

Choose a reason for hiding this comment

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

because you are loading app/bootstrap.php.cache which loads it. And the goal of this chapter is precisely to explain you how to do to skip the app/bootstrap.php.cache


use Symfony\Component\HttpFoundation\Request;
Expand Down
5 changes: 2 additions & 3 deletions cookbook/form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,8 @@ great, your user can't actually add any new tags yet.
This directive limits recursion to 100 calls which may not be enough for
rendering the form in the template if you render the whole form at
once (e.g ``form_widget(form)``). To fix this you can set this directive
to a higher value (either via a PHP ini file or via :phpfunction:`ini_set`,
for example in ``app/autoload.php``) or render each form field by hand
using ``form_row``.
to a higher value (either via a PHP ini file or via :phpfunction:`ini_set`)
or render each form field by hand using ``form_row``.
Copy link
Member

Choose a reason for hiding this comment

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

Please keep this as it is still a place where you can do it. This file still exist in a Symfony project


.. _cookbook-form-collections-new-prototype:

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
3 changes: 1 addition & 2 deletions quick_tour/the_architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ This class must implement two methods:
(more on this later).

Autoloading is handled automatically via `Composer`_, which means that you
can use any PHP classes without doing anything at all! If you need more flexibility,
you can extend the autoloader in the ``app/autoload.php`` file. All dependencies
can use any PHP classes without doing anything at all! All dependencies
Copy link
Member

Choose a reason for hiding this comment

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

Please keep the sentence explaining you can extend the autoloader as it is still true

are stored under the ``vendor/`` directory, but this is just a convention.
You can store them wherever you want, globally on your server or locally
in your projects.
Expand Down