Skip to content

Add Custom Service

Add Custom Service

Overview

In this guide you'll learn how to create a custom service using the Symfony DI Container.

Prerequisites

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

Adding service

For adding a custom service, you need to provide a services.php file in your plugin. Place a file with name services.php into a directory called src/Resources/config/.

php
<?php declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $configurator): void {
    $services = $configurator->services();
};

Now you have two possibilities to add a service to your plugin.

Using autowire and autoconfigure

Set autowire and autoconfigure to true in your services.php file. Symfony will then automatically register your service. Read more about it in the Symfony docs.

php
<?php declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $configurator): void {
    $services = $configurator->services()
        ->defaults()
            ->autowire()
            ->autoconfigure();

    $services->load('Swag\\BasicExample\\', '../../')
        ->exclude('../../{Resources,Migration,*.php}');
};

Now every PHP class in the src directory of your plugin will be registered as a service. The directory Resources and Migration are excluded, as they usually should not contain services.

Explicit declaration

Instead of autowiring and autoconfiguring, you can also declare your service explicitly. Use this option if you want to have more control over your service.

php
<?php declare(strict_types=1);

use Swag\BasicExample\Service\ExampleService;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $configurator): void {
    $services = $configurator->services();

    $services->set(ExampleService::class);
};

Actual service class

Then this is what your service could look like:

php
<?php declare(strict_types=1);

namespace Swag\BasicExample\Service;

class ExampleService
{
    public function doSomething(): void
    {
        ...
    }
}

INFO

By default, all services in Shopware 6 are marked as private. Read more about private and public services.

Alternatives to PHP configuration

Symfony supports two other file formats to define your services: YAML and XML. However, starting with Symfony 7.4, XML service configuration has been deprecated, and it will no longer be supported in Symfony 8.0. Choose the one that suits you best.

Next steps

You have now created your own custom service. In the same manner, you can create other important plugin classes, such as commands, scheduled tasks or a subscriber to listen to events.

Furthermore, we also have a guide explaining how to customize an existing service instead.

Was this page helpful?
UnsatisfiedSatisfied
Be the first to vote!
0.0 / 5  (0 votes)