Skip to content

Commit b46ff01

Browse files
javiereguiluzxabbuh
authored andcommitted
Explained the locateResource() method of HttpKernel
1 parent 82f6cfc commit b46ff01

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

bundles/best_practices.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,21 @@ API is being used. The following code, would work for *all* users::
463463
}
464464
}
465465

466+
Resources
467+
---------
468+
469+
If the bundle references any resources (config files, translation files, etc.),
470+
don't use physical paths (e.g. ``__DIR__/config/services.xml``) but logical
471+
paths (e.g. ``@AppBundle/Resources/config/services.xml``).
472+
473+
The logical paths are required because of the bundle overriding mechanism that
474+
lets you override any resource/file of any bundle. See :ref:`http-kernel-resource-locator`
475+
for more details about transforming physical paths into logical paths.
476+
477+
Beware that templates use a simplified version of the logical path showed above.
478+
For example, a ``index.html.twig`` template located in the ``Resources/views/Default/``
479+
directory of the AppBundle, is referenced as ``@App/Default/index.html.twig``.
480+
466481
Learn more
467482
----------
468483

bundles/override.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ How to Override any Part of a Bundle
77
This document is a quick reference for how to override different parts of
88
third-party bundles.
99

10+
.. tip::
11+
12+
The bundle overriding mechanism means that you cannot use physical paths to
13+
refer to bundle's resources (e.g. ``__DIR__/config/services.xml``). Always
14+
use logical paths in your bundles (e.g. ``@AppBundle/Resources/config/services.xml``)
15+
and call the :ref:`locateResource() method <http-kernel-resource-locator>`
16+
to turn them into physical paths when needed.
17+
1018
Templates
1119
---------
1220

components/http_kernel.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ each event has their own event object:
592592
===================== ================================ ===================================================================================
593593
Name ``KernelEvents`` Constant Argument passed to the listener
594594
===================== ================================ ===================================================================================
595-
kernel.request ``KernelEvents::REQUEST`` :class:`Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent`
595+
kernel.request ``KernelEvents::REQUEST`` :class:`Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent`
596596
kernel.controller ``KernelEvents::CONTROLLER`` :class:`Symfony\\Component\\HttpKernel\\Event\\FilterControllerEvent`
597597
kernel.view ``KernelEvents::VIEW`` :class:`Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent`
598598
kernel.response ``KernelEvents::RESPONSE`` :class:`Symfony\\Component\\HttpKernel\\Event\\FilterResponseEvent`
@@ -701,6 +701,32 @@ look like this::
701701
// ...
702702
}
703703

704+
.. _http-kernel-resource-locator:
705+
706+
Locating Resources
707+
------------------
708+
709+
The HttpKernel component is responsible of the bundle mechanism used in Symfony
710+
applications. The key feature of the bundles is that they allow to override any
711+
resource used by the application (config files, templates, controllers,
712+
translation files, etc.)
713+
714+
This overriding mechanism works because resources are referenced not by their
715+
physical path but by their logical path. For example, the ``services.xml`` file
716+
stored in the ``Resources/config/`` directory of a bundle called AppBundle is
717+
referenced as ``@AppBundle/Resources/config/services.xml``. This logical path
718+
will work when the application overrides that file and even if you change the
719+
directory of AppBundle.
720+
721+
The HttpKernel component provides a method called :method:`Symfony\\Component\\HttpKernel\\Kernel::locateResource`
722+
which can be used to transform logical paths into physical paths::
723+
724+
use Symfony\Component\HttpKernel\HttpKernel;
725+
726+
// ...
727+
$kernel = new HttpKernel($dispatcher, $resolver);
728+
$path = $kernel->locateResource('@AppBundle/Resources/config/services.xml');
729+
704730
Learn more
705731
----------
706732

0 commit comments

Comments
 (0)