Bootstrap services

Besides services.yaml, a module may provide an optional bootstrap-services.yaml file in its root directory. Services registered there are loaded for every installed module, independently of whether the module is activated.

How it differs from services.yaml

File

Loaded when

Typical content

services.yaml

the module is activated

the regular services of the module

bootstrap-services.yaml

the module is installed (also while it is deactivated)

services that must exist before or around activation (see below)

When a module is installed, its bootstrap-services.yaml is imported into the shop’s generated project services (the same way a component’s services.yaml is registered), and removed again on uninstall. It is therefore loaded into the shop container on every request, regardless of the module’s activation state.

When to use it

Use bootstrap-services.yaml only for services that have to be present independently of the module’s activation state:

  • Module lifecycle event subscribers — react to your own module being activated or deactivated. The subscriber has to be in the container already when activation happens, which is only possible from bootstrap-services.yaml. See Module Events.

  • Tagged migration path providers — expose your module’s migrations so they can be run before the module is activated (see Database Migration).

When not to use it

Warning

Services declared in bootstrap-services.yaml are loaded even while the module is deactivated. Do not put the regular functionality of your module there — a deactivated module must not influence the shop. Regular services belong in services.yaml, which is only loaded while the module is active.

Registering a service

Services are registered exactly as in services.yaml, but in the bootstrap-services.yaml file in the module root. For example, a module lifecycle event subscriber:

services:
    _defaults:
        public: false
        autowire: true

    MyVendor\MyModule\Setup\ModuleLifecycleSubscriber:
        tags:
            - { name: kernel.event_subscriber }

Note

A service registered here runs for every installed module, so it must stay inert until its trigger. A lifecycle subscriber must ignore events that belong to other modules — see Module Events for a complete example.