---
title: Introduction to the Basic Architectural Pattern | Shopware Community Hub
description: Get an overview of the basic architectural pattern used in Shopware.
canonical_url: >-
  https://hub.shopware.com/learn/unit/introduction-to-the-basic-architectural-pattern
---

<LearningObjectives>

- Learn the roles of **services**, **controllers**, and **event subscribers** in a Shopware plugin.
- Learn why classes declare their dependencies instead of creating them directly.
- Understand how Shopware's **Dependency Injection container** creates and connects services.

</LearningObjectives>

# Introduction to the Basic Architectural Pattern

In the previous learning unit, you created a minimal plugin. The plugin gives your code a place inside Shopware, but it does not yet explain how the different PHP classes work together.

Shopware is built on Symfony and uses many of its architectural patterns. A typical Shopware plugin separates HTTP requests, business logic, and reactions to events into different classes. The Dependency Injection container connects these classes.

This learning unit gives you a first overview of that structure. You do not need to memorize every detail yet. In the next learning units, you will use these components in practice.

## 1. Get to Know the Main Components

The following diagram shows the main components covered in this learning unit and how they relate to each other:

![Backend: Basic Archtectural Overview](assets/images/backend-basic-architectural-overview.jpg)

The basic idea is:

- **Controllers** receive HTTP requests and return responses.
- **Event subscribers** react when a specific Shopware or Symfony event occurs.
- **Services** contain reusable business logic.
- The **Dependency Injection container** creates these objects and provides their dependencies.

Controllers and event subscribers are two different entry points into your plugin. Both can delegate work to the same service instead of implementing the same business logic themselves.

### 1.1.: Services: Reusable Business Logic

A service is a PHP class that performs a specific task. For example, a service can process order data, calculate a value, call an external API, or send a notification.

Keeping this logic in a service has an important advantage: Different parts of your plugin can reuse it. For example, both a controller and an event subscriber can use an `OrderService` instead of implementing the same order logic themselves.

### 1.2.: Controllers: HTTP Entry Points

A controller is an entry point for an HTTP request. It receives the request, delegates work to the appropriate service, and returns a response.

The controller should coordinate the request instead of containing all business logic itself. If a request should send an order email, the controller can pass that task to `OrderService`.

### 1.3.: Event Subscribers: Event Entry Points

Shopware and Symfony dispatch events at many points during a request or business process. An event subscriber listens for selected events and runs a method when one of them occurs.

For example, a subscriber can react after an order is placed or after a product page has been loaded. This lets your plugin add behavior without changing Shopware's core code.

For this, the subscriber implements `EventSubscriberInterface`. Its `getSubscribedEvents()` method defines which event should call which method. The called method can then delegate its business task to a service.

## 2. Object Creation Without Dependency Injection

In object-oriented PHP, one class often needs other objects to do its work. A controller may need an order service, while that order service may need a mailer and a logger.

Without Dependency Injection, each consumer would have to build the complete object structure itself. The controller could look like this:

```php
// Simplified example without a DI container
$orderService = new OrderService(
    new Mailer(new SmtpClient('host', 'user', 'pass')),
    new Logger('/var/log/app.log'),
);

$orderService->sendMail();
```

If the subscriber also needs `OrderService`, it has to repeat the same construction:

```php
// The same setup is repeated in another consumer
$orderService = new OrderService(
    new Mailer(new SmtpClient('host', 'user', 'pass')),
    new Logger('/var/log/app.log'),
);

$orderService->sendMail();
```

Both consumers only want to use `OrderService`, but they also need to know how to create the mailer, SMTP client, and logger. If the construction of `OrderService` changes, every repeated setup may need to change as well.

This growing amount of repeated construction code is the reason Dependency Injection containers became an established part of application frameworks. They move the responsibility for creating and connecting objects out of the consuming classes.

## 3. Object Creation With Dependency Injection

Dependency Injection separates these responsibilities:

- A class declares **what it needs** in its constructor.
- The service configuration describes **how the objects are connected**.
- The DI container creates the objects and passes them to the consuming classes.

The DI container does not decide when `sendMail()` is called. That remains the responsibility of the controller or subscriber.

### 3.1.: Dependencies Declared in PHP

With Dependency Injection, `OrderService` declares its own dependencies through its constructor:

```php
<?php declare(strict_types=1);

namespace TestPlugin\Service;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\MailerInterface;

class OrderService
{
    public function __construct(
        private readonly MailerInterface $mailer,
        private readonly LoggerInterface $logger,
    ) {
    }

    public function sendMail(): void
    {
        // Send the mail and write a log entry.
    }
}
```

The controller only declares that it needs `OrderService`:

```php
<?php declare(strict_types=1);

namespace TestPlugin\Controller;

use TestPlugin\Service\OrderService;

class OrderController
{
    public function __construct(
        private readonly OrderService $orderService,
    ) {
    }

    public function sendOrderMail(): void
    {
        $this->orderService->sendMail();
    }
}
```

The event subscriber can receive the same service:

```php
<?php declare(strict_types=1);

namespace TestPlugin\Subscriber;

use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use TestPlugin\Service\OrderService;

class OrderSubscriber implements EventSubscriberInterface
{
    public function __construct(
        private readonly OrderService $orderService,
    ) {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlacedEvent',
        ];
    }

    public function onCheckoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event): void
    {
        $this->orderService->sendMail();
    }
}
```

The controller and subscriber now declare **what** they need. Neither class knows **how** `OrderService` or its dependencies are created.

### 3.2.: Service Wiring in `services.php`

Shopware reads the service configuration when it builds the DI container. For plugins created with Shopware 6.7.14.0 or newer, this configuration is generated in `src/Resources/config/services.php`.

The following configuration connects the classes from the previous examples:

```php
<?php declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use TestPlugin\Controller\OrderController;
use TestPlugin\Service\OrderService;
use TestPlugin\Subscriber\OrderSubscriber;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

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

    $services->set(OrderService::class)
        ->args([
            service('mailer'),
            service('logger'),
        ]);

    $services->set(OrderController::class)
        ->public()
        ->args([
            service(OrderService::class),
        ]);

    $services->set(OrderSubscriber::class)
        ->args([
            service(OrderService::class),
        ])
        ->tag('kernel.event_subscriber');
};
```

Read the configuration from the dependencies outward:

1. `OrderService` receives Shopware's mailer and logger services.
2. `OrderController` receives the configured `OrderService`.
3. `OrderSubscriber` receives the same configured `OrderService`.

The `kernel.event_subscriber` tag has a separate purpose. It tells Symfony to register `OrderSubscriber` as an event subscriber. The constructor argument provides its dependency; the tag activates its role in the event system.

<Callout title="Service Configuration in Existing Plugins" type="info">

New plugins created with Shopware 6.7.14.0 or newer use `services.php`. Existing plugins may still contain `services.xml`. XML service configuration still works in Shopware 6.7, but it is deprecated and will no longer be loaded in Shopware 6.8.

Both files describe the same service wiring in a different syntax. This course uses the current `services.php` format. The previous XML configuration is included below so that you can recognize it in existing plugins.

</Callout>

<CollapsibleGroup>

<CollapsibleSection title="The Same Configuration in services.xml">

Before Shopware 6.7.14.0, newly created plugins used `services.xml`. The same example looked like this:

```xml
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="TestPlugin\Service\OrderService">
            <argument type="service" id="mailer"/>
            <argument type="service" id="logger"/>
        </service>

        <service id="TestPlugin\Controller\OrderController" public="true">
            <argument type="service" id="TestPlugin\Service\OrderService"/>
        </service>

        <service id="TestPlugin\Subscriber\OrderSubscriber">
            <argument type="service" id="TestPlugin\Service\OrderService"/>
            <tag name="kernel.event_subscriber"/>
        </service>
    </services>
</container>
```

The PHP and XML examples define the same three services, constructor arguments, and subscriber tag. They are alternatives, so do not define the same services in both files.

</CollapsibleSection>

</CollapsibleGroup>

## 4. How the Components Work Together

The complete flow is now visible: A controller or subscriber receives `OrderService` from the DI container and decides when to call it. `OrderService` receives its own dependencies from the same container and can focus on its business task.

A controller and an event subscriber enter this flow in different ways. A controller reacts to an HTTP request, while a subscriber reacts to an event. In both cases, they can delegate the actual business task to the same `OrderService`.

Before either class uses the service, the DI container prepares the required objects:

1. The service configuration tells the container how the classes are connected.
2. The container creates `OrderService` with its mailer and logger dependencies.
3. It injects the configured `OrderService` into the controller and subscriber.
4. The controller or subscriber decides when to call `sendMail()`.

This separates object construction from application behavior. The DI container is responsible for creating and connecting the objects. The controller, subscriber, and service remain responsible for what happens during a request or event.

<ArticleQuestionnaire>
  <ArticleQuestionnaireQuestion>Which component is the best place for reusable business logic?</ArticleQuestionnaireQuestion>
  <ArticleQuestionnaireAnswer>Controller</ArticleQuestionnaireAnswer>
  <ArticleQuestionnaireAnswer correct>Service</ArticleQuestionnaireAnswer>
  <ArticleQuestionnaireAnswer>Event</ArticleQuestionnaireAnswer>
</ArticleQuestionnaire>

<ArticleQuestionnaire>
  <ArticleQuestionnaireQuestion>What does the Dependency Injection container do?</ArticleQuestionnaireQuestion>
  <ArticleQuestionnaireAnswer>It decides when a controller or subscriber calls a service method.</ArticleQuestionnaireAnswer>
  <ArticleQuestionnaireAnswer correct>It creates objects, connects their dependencies, and provides them to consuming classes.</ArticleQuestionnaireAnswer>
  <ArticleQuestionnaireAnswer>It stores the business data used by services.</ArticleQuestionnaireAnswer>
</ArticleQuestionnaire>

## Summary

In this learning unit, you learned that:

- Services contain reusable business logic.
- Controllers handle HTTP requests and delegate work to services.
- Event subscribers react to selected Shopware or Symfony events.
- Classes declare their dependencies through constructor arguments.
- The DI container creates and connects the required objects based on `services.php`.

You now have the basic architectural overview needed for the next learning unit, where you will add a storefront controller to your plugin and use these concepts in practice.
