Service Container

The OXID eShop uses the Symfony DI Container to handle services. For more information about DI Container refer to the Symfony DI Container documentation

Services can be used in modules and OXID eShop components. You can use services in multiple ways:

Creating services

There is a step by step instructions for modules but the instructions are the same for components. Modules need to be activated in order to make the services included working in contrast to components which just need to be installed by composer.

Getting services

  1. Getting services via injecting it through the constructor as a dependency. Use this way whenever possible (inside another service).

  2. Getting services using service locator. You can use ContainerFactory class to get a service. The service you want to get has to be marked as public.

    Example:

    $containerFactory = ContainerFactory::getInstance();
    $container = $containerFactory->getContainer();
    $moduleActivationService = $container->get(ModuleActivationBridgeInterface::class);
    

Important

Please consider the hints in the section Stable OXID eShop Core Services.

Replacing OXID eShop services in a project

In some cases you might need to change system services behaviour. Creating a replacement for OXID eShop system services should be done in a OXID eShop Component and not in a module.

You can overwrite system services in your project. For this purpose there is a file named configurable_services.yaml, which you will find (or will have to create) under var/configuration. This file exists exactly once per project. Do not use any other way of replacing OXID eShop services!

Example of var/configuration/configurable_services.yaml file:

services:
  Psr\Log\LoggerInterface:
    class: MyProject\CustomLogger
    public: true

In the example, the implementation MyProjectCustomLogger is chosen for the service with the key PsrLogLoggerInterface and replaces the original one.

Note

There are several possibilities to configure the Symfony DI container. OXID framework only uses and supports the yaml file format.

Important

Please consider the hints in the section Stable OXID eShop Core Services.

Stable OXID eShop Core Services

We do not recommend to use or overwrite system services in internal directory, unless services have @stable annotation. Services which are not marked as stable might change more often in future releases. For more information refer to README.md file in internal directory.

Service Container Cache

Normally, the container factory will get the container from a container cache file. It resides in the tmp directory of your application and is called container_cache.php.

If this file is not found, the container will be set up fresh from it’s configuration. If you change something in the container configuration, you need to delete container_cache.php to get a container that reflects your changes.