Skip to content

Explained the locateResource() method of HttpKernel #7909

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

Closed
wants to merge 4 commits into from
Closed
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
15 changes: 15 additions & 0 deletions bundles/best_practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,21 @@ API is being used. The following code, would work for *all* users::
}
}

Resources
---------

If the bundle references any resources (config files, translation files, etc.),
don't use physical paths (e.g. ``__DIR__/config/services.xml``) but logical
paths (e.g. ``@AppBundle/Resources/config/services.xml``).

The logical paths are required because of the bundle overriding mechanism that
lets you override any resource/file of any bundle. See :ref:`http-kernel-resource-locator`
for more details about transforming physical paths into logical paths.

Beware that templates use a simplified version of the logical path showed above.
Copy link
Member

Choose a reason for hiding this comment

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

shown

For example, a ``index.html.twig`` template located in the ``Resources/views/Default/``
Copy link
Member

Choose a reason for hiding this comment

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

an

directory of the AppBundle, is referenced as ``@App/Default/index.html.twig``.

Learn more
----------

Expand Down
8 changes: 8 additions & 0 deletions bundles/override.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ How to Override any Part of a Bundle
This document is a quick reference for how to override different parts of
third-party bundles.

.. tip::

The bundle overriding mechanism means that you cannot use physical paths to
refer to bundle's resources (e.g. ``__DIR__/config/services.xml``). Always
use logical paths in your bundles (e.g. ``@AppBundle/Resources/config/services.xml``)
and call the :ref:`locateResource() method <http-kernel-resource-locator>`
to turn them into physical paths when needed.

Templates
---------

Expand Down
28 changes: 27 additions & 1 deletion components/http_kernel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ each event has their own event object:
===================== ================================ ===================================================================================
Name ``KernelEvents`` Constant Argument passed to the listener
===================== ================================ ===================================================================================
kernel.request ``KernelEvents::REQUEST`` :class:`Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent`
kernel.request ``KernelEvents::REQUEST`` :class:`Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent`
kernel.controller ``KernelEvents::CONTROLLER`` :class:`Symfony\\Component\\HttpKernel\\Event\\FilterControllerEvent`
kernel.view ``KernelEvents::VIEW`` :class:`Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent`
kernel.response ``KernelEvents::RESPONSE`` :class:`Symfony\\Component\\HttpKernel\\Event\\FilterResponseEvent`
Expand Down Expand Up @@ -701,6 +701,32 @@ look like this::
// ...
}

.. _http-kernel-resource-locator:

Locating Resources
------------------

The HttpKernel component is responsible of the bundle mechanism used in Symfony
applications. The key feature of the bundles is that they allow to override any
resource used by the application (config files, templates, controllers,
translation files, etc.)

This overriding mechanism works because resources are referenced not by their
physical path but by their logical path. For example, the ``services.xml`` file
stored in the ``Resources/config/`` directory of a bundle called AppBundle is
referenced as ``@AppBundle/Resources/config/services.xml``. This logical path
will work when the application overrides that file and even if you change the
directory of AppBundle.

The HttpKernel component provides a method called :method:`Symfony\\Component\\HttpKernel\\Kernel::locateResource`
which can be used to transform logical paths into physical paths::

use Symfony\Component\HttpKernel\HttpKernel;

// ...
$kernel = new HttpKernel($dispatcher, $resolver);
$path = $kernel->locateResource('@AppBundle/Resources/config/services.xml');

Learn more
----------

Expand Down