Skip to content

Dependency injection

Dependency injection


In this guide you'll learn how to inject services into other services. You can read more about injecting services in the Symfony documentation.


In order to add your own custom service for your plugin, you first need a plugin as base. Therefore, you can refer to the Plugin Base Guide.

Furthermore, you need a working service. Therefore, you can refer to Adding a custom service guide.

Injecting another service

Let's get started with an example how to inject a service. This example will be about injecting the SystemConfigService into our ExampleService.

Here's our example services.xml:

// <plugin root>/src/Resources/config/services.xml
<?xml version="1.0" ?>

<container xmlns=""

        <service id="Swag\BasicExample\Service\ExampleService">
            <argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>

Now we have to add the injected service as argument to our service constructor.

In the following you can find our ExampleService where we injected the SystemConfigService with an example function getShopname() where we use it.

// <plugin root>/src/Service/ExampleService.php
<?php declare(strict_types=1);

namespace Swag\BasicExample\Service;

use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;

class ExampleService
    * @var SystemConfigService 
    private $systemConfigService;

    public function __construct(SystemConfigService $systemConfigService): void
        $this->systemConfigService = $systemConfigService;

    public function getShopname(SalesChannelContext $context): string
        return $this->systemConfigService->getString('core.basicInformation.shopName', $context->getSalesChannel()->getId());