Skip to content

Add Custom Service

Add Custom Service ​

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

Prerequisites ​

This guide builds on the Plugin Base Guide.

Register a service ​

Create a services.php file at src/Resources/config/services.php in your plugin.

php
<?php declare(strict_types=1);

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

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

There are two approaches:

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 ​

Example service class:

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.

Next steps ​

You can apply the same approach to register other plugin classes, such as commands, scheduled tasks, or a subscriber to listen to events.

See also: Adjusting a service.

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