Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

symfony-ddd-example

> 编程语言
Open source

DDD + Hexagonal Architecture + CQRS with Symfony | 195⭐ community reference

197 stars0 likes1 views
WebsiteGitHub

About

DDD + Hexagonal Architecture + CQRS with Symfony | 195⭐ community reference

DDD, Hexagonal Architecture & CQRS with Symfony and Doctrine

Example of a Symfony application using Domain-Driven Design (DDD) and Command Query Responsibility Segregation (CQRS) principles keeping the code as simple as possible.

Environment Setup

Needed tools

  1. Install Docker
  2. Clone this project: git clone https://github.com/salletti/symfony-ddd-example.git
  3. Move to the project folder: cd symfony-ddd-example

Application execution

  1. If not already done, install Docker Compose
  2. Run docker-compose build --pull --no-cache to build fresh images
  3. Run docker-compose up (the logs will be displayed in the current shell)
  4. Open https://localhost in your favorite web browser and accept the auto-generated TLS certificate
  5. Run docker-compose down --remove-orphans to stop the Docker containers.

Project explanation

A very simple application (absolutely not complete) to manage a blog. The goal is to show how to initialize a project with a DDD structure and how to make the different Bounded Contexts communicate.

Structure

…

Hexagonal Architecture

This projects follows the Hexagonal Architecture pattern.

The application layer can only use domain implementations and the infrastructure layer must implement domain interfaces in order to be completely independent.

For example the repository which is closely related to Doctrine and which is located in the infrastructure layer implements a domain interface.

//App\Blog\Post\Domain\Repository\ArticleRepositoryInterface

interface ArticleRepositoryInterface { public function find($id, $lockMode = null, $lockVersion = null);

public function save(Article $article): void;

}

CQRS

Inside the Application / Model directory of the Bounded Contexts there are the "Command" or "Query" depending on the action to be performed. These are dispatched via the Symfony message bus which will forward them to the associated service handler.

Example of Command:

//App\Blog\Post\Application\EventSubscriber\OnPublicationApprovedEventSubscriber

$createArticleCommand = new CreateArticleCommand(); $createArticleCommand->setTitle($event->getTitle()); $createArticleCommand->setBody($event->getBody()); $createArticleCommand->setAuthor($event->getAuthor()); $createArticleCommand->setCategory($event->getCategoryId());

$this->messageBus->dispatch($createArticleCommand);

Example of Query

//App\Blog\Post\Application\Controller\Api\GetArticleController

$article = $this->handle(new FindArticleQuery($id));

Repository pattern

Our repositories try to be as simple as possible usually only containing 2 methods save and find.

Bounded Contexts

There are three different Bounded Contexts:

  • Post: It takes care of managing everything related to an article.
  • User: Here you should find everything related to user management.
  • Category: This is where you can create categories, edit or delete them (even if only the creation is implemented).

Aggregates

Each BC has one and only one Aggregate Root.

An Aggregate is a cluster of associated object that we treat as a single unit. Each Aggregate has a single root Entity and a boundary that marks what is inside and what is outside the Aggregate.

A Comment object only really make sense in the context of a Article because we can’t have a Comment without a Article.

The root Entity is the only Entity that is globally accessible in the application. In the example from above, Article would be the root Entity.

Inside the boundary we would have all the associated objects of this Aggregate. For example, we would have the CommentEntity as well as any other Entities or Value Objects that make up the Aggregate.

The Comment object can hold references to each other internally to the Aggregate, but no other external object can hold a reference to any object internally to the Aggregate that is not the root Entity.

The only way to access the Comment Entity is to retrieve the Article root Entity and traverse it’s associated objects.

Communication between Bounded Contexts

The BCs must be independent, this means that they must not know anything about the other BCs. For this reason, communication between BCs should ideally be done with an event-driven system.

In our example, when we want to create an article, the payload contains the category slug and the user id. But how do we get the category id and make sure the user id is correct? This data is managed in other bounded contexts.

Below is the sequence diagram showing the steps to publish an article.

An OnPublicationRequestedEventis dispatched and listened in the user BC which check if the user exists. The UserVerifiedEventevent is then dispatched and listened in the category BC which retrieves the category id by the slug. A new OnPublicationApprovedEventis then dispatched and listened in the Post BC. Now the creation of the article can be done.

How to use

  1. $ docker-compose exec php sh

  2. Create user: $ bin/console app:create-user [email protected] p4$$word ROLE_EDITOR

  3. Create category: POST https://localhost/api/categories/

    { "name": "Sport", "slug": "sport" }

  4. Create Article: POST https://localhost/api/articles/

    { "title": "article", "body": "body", "author": author_id, "categorySlug": "sport" }

  5. Create Comment:

    POST https://localhost/api/comments/

    { "article_id": author_id, "email": "[email protected]", "message": "test message" }

  6. Get Article:

    GET https://localhost/api/articles/*author_id*

About Me

  • Blog
  • Linkedin
  • Twitter
  • Medium

Resources

  • https://matthiasnoback.nl/2018/06/doctrine-orm-and-ddd-aggregates/
  • https://github.com/CodelyTV/php-ddd-example
  • https://latteandcode.medium.com/chapter-1-what-i-learned-from-ddd-basic-concepts-db887c397599
  • https://latteandcode.medium.com/cap%C3%ADtulo-2-lo-que-aprend%C3%AD-de-ddd-value-objects-b5a28c2298cc
  • https://latteandcode.medium.com/cap%C3%ADtulo-3-lo-que-aprend%C3%AD-de-ddd-entidades-a61cdb4b686
  • https://latteandcode.medium.com/cap%C3%ADtulo-4-lo-que-aprend%C3%AD-de-ddd-servicios-a427413f4108
  • https://latteandcode.medium.com/cap%C3%ADtulo-5-eventos-de-dominio-6743439a72bd
  • https://latteandcode.medium.com/cap%C3%ADtulo-6-lo-que-aprend%C3%AD-de-ddd-m%C3%B3dulos-y-bounded-contexts-6a516fb52c12
  • https://blog.engineering.publicissapient.fr/2018/06/25/craft-les-patterns-tactiques-du-ddd/
  • https://www.mirkosertic.de/blog/2013/04/domain-driven-design-example/
  • https://www.culttt.com/2014/12/17/aggregates-domain-driven-design/
  • https://speakerdeck.com/nealio82/the-absolute-beginners-guide-to-ddd-with-symfony
  • http://wellfreire.github.io/en/symfony2-and-ddd-there-is-code-beyond-bundles/

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言