diff --git a/bundles/best_practices.rst b/bundles/best_practices.rst index 44e65e1d66c..d4a68f29fb9 100644 --- a/bundles/best_practices.rst +++ b/bundles/best_practices.rst @@ -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. +For example, a ``index.html.twig`` template located in the ``Resources/views/Default/`` +directory of the AppBundle, is referenced as ``@App/Default/index.html.twig``. + Learn more ---------- diff --git a/bundles/override.rst b/bundles/override.rst index 37c38481472..52950a29c3c 100644 --- a/bundles/override.rst +++ b/bundles/override.rst @@ -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 ` + to turn them into physical paths when needed. + Templates --------- diff --git a/components/http_kernel.rst b/components/http_kernel.rst index e2f9f251583..5ef1a3e02e1 100644 --- a/components/http_kernel.rst +++ b/components/http_kernel.rst @@ -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` @@ -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 ----------