Skip to content
Open
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
7 changes: 7 additions & 0 deletions bundles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ Start by creating a new class called ``AcmeBlogBundle``::
{
}

.. tip::

If your bundle is designed for an application that does not use the
HttpKernel component (e.g. a console tool or worker), you can extend
:class:`Symfony\\Component\\DependencyInjection\\Kernel\\AbstractBundle`
instead. See :ref:`building-http-less-applications` for more details.

.. warning::

If your bundle must be compatible with previous Symfony versions you have to
Expand Down
64 changes: 64 additions & 0 deletions configuration/front_controllers_and_kernel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,70 @@ includes the following:
You can change the cache directory location and name. For more information
read the article :doc:`/configuration/override_dir_structure`.

.. _building-http-less-applications:

Building HTTP-less Applications
-------------------------------

.. versionadded:: 8.1

The DependencyInjection Kernel infrastructure was introduced in Symfony 8.1.

If your application does not handle HTTP requests (e.g. a console tool, a
message consumer or a background worker), you can build a kernel that uses the
full Symfony dependency injection infrastructure without requiring the HttpKernel
component.

The DependencyInjection component provides a set of classes in the
``Symfony\Component\DependencyInjection\Kernel`` namespace that mirror the
traditional HttpKernel-based classes:

================================================= ================================================================
HTTP-less (DependencyInjection) HTTP (HttpKernel)
================================================= ================================================================
``DependencyInjection\Kernel\KernelInterface`` ``HttpKernel\KernelInterface``
``DependencyInjection\Kernel\AbstractKernel`` ``HttpKernel\Kernel``
``DependencyInjection\Kernel\KernelTrait`` ``FrameworkBundle\Kernel\MicroKernelTrait``
``DependencyInjection\Kernel\AbstractBundle`` ``HttpKernel\Bundle\AbstractBundle``
``DependencyInjection\Kernel\BundleInterface`` ``HttpKernel\Bundle\BundleInterface``
================================================= ================================================================

To create an HTTP-less kernel, extend
:class:`Symfony\\Component\\DependencyInjection\\Kernel\\AbstractKernel` and use the
:class:`Symfony\\Component\\DependencyInjection\\Kernel\\KernelTrait`::

// src/Kernel.php
namespace App;

use Symfony\Component\DependencyInjection\Kernel\AbstractKernel; // new in 8.1
use Symfony\Component\DependencyInjection\Kernel\KernelTrait; // new in 8.1

class Kernel extends AbstractKernel
{
use KernelTrait;

// The KernelTrait provides a default configureContainer() method
// that loads config/packages/*.{php,yaml} and config/services.{yaml,php}.
// Override it only if you need a custom configuration setup.
}

The ``KernelTrait`` provides the same container lifecycle as ``MicroKernelTrait``
(building, compiling, caching the service container) but without any HTTP-related
logic. It follows the same conventions: ``config/bundles.php`` for bundle
registration, ``config/packages/`` for configuration and ``config/services.yaml``
(or ``.php``) for service definitions.

Code that only needs to interact with the kernel for dependency injection
purposes (bundles, container, environment) can now type-hint
``Symfony\Component\DependencyInjection\Kernel\KernelInterface``
instead of ``Symfony\Component\HttpKernel\KernelInterface``.

.. tip::

The ``getLogDir()`` method is nullable on ``KernelTrait``. To disable
the log directory (e.g. for a console-only application), set the
``APP_LOG_DIR`` environment variable to ``false``.

.. _`front controller`: https://en.wikipedia.org/wiki/Front_Controller_pattern
.. _`decorate`: https://en.wikipedia.org/wiki/Decorator_pattern
.. _Debug component: https://github.com/symfony/debug
Loading