Structure of a service file

Last updated on
28 February 2017

This documentation is incomplete. Add more information.

Services are defined in a file example.services.yml (assuming the module is named 'example'). When this file is placed in the root of the module, it will automatically be detected and used by Drupal.

Example of a example.services.yml file:

services:

  # Defines a simple service of which requires no parameter for its constructor.
  example.simple:
    class: Drupal\Example\Simple

  # Defines a service which requires the module_handler for its constructor.
  example.with_module_handler
    class: Drupal\Example\WithModuleHandler
    arguments: ['@module_handler']

More examples can be found in core.services.yml or any module's service file. All definitions are aggregated and saved as a serialized array in the database.

Properties of a service

  • abstract: The service definition will not result in an actual service. The service is supposed to be used as 'parent'. Values: ‘true’ = service will be abstract; ‘false’ (default) = service will be instantiated.
  • alias: Alias name for a service. More information.
  • arguments: Argument(s) to be used with the factory method (in case of ‘factory_class’) or with the class constructor (in case of ‘class’). A '@' indicates another service, and then what is put in here is the name of the service, as defined in its services.yml file. An argument wrapped in '%' characters represents a parameter, which can be defined in the 'parameters' section of the file. More information.
  • calls: Used for setter injection. Defines additional methods to call after the service has been instantiated. More information.
  • class: The class of the service.
  • configurator: A callable which configures a service after its instantiation. More information.
  • factory_class: The class (and factory_method) that will instantiate the service class. More information.
  • factory_method: The method that will be called to instantiate the service class.
  • factory_service: The service (and factory_method) that will instantiate the service class.
  • file: A file which is included just before the service gets loaded. More information.
  • parent: A service definition which properties will be inherited. See 'abstract'. More information.
  • properties: More information.
  • public: Set the service as public or private. Private services can only be used as argument for other services. More information.
    ‘true’ (default): service is public. ‘false’: service is private.
  • scope: Determines how long an instance of a service is used by the container. ‘container’ (default): always use the same instance. ‘prototype’: create a new instance every time the service is called. ‘request’: create a new instance at each (sub) request. More information.
  • synchronized: The service will be reconfigured on each scope change (deprecated starting from Symfony 2.7) More information.
  • synthetic: The service is injected into the container instead of being created by the container. Values: ‘true’ = synthetic service; ‘false’ (default) = normal service. More information.
  • tags: A name to identify groups of services. Tags are used in compiler passes and for collecting cache bins. More information.