Voltar ao ranking

CodelyTV/php-ddd-example

PHPpro.codely.tv/library/ddd-en-php

🐘🎯 Hexagonal Architecture + DDD + CQRS in PHP using Symfony 7

cqrscodelytvdddphpsymfonybehatphpunitdoctrinedomain-driven-designdockertestinghexagonal-architecture
Crescimento de estrelas
Estrelas
3.1k
Forks
1.1k
Crescimento semanal
Issues
23
1k2k3k
nov. de 2016jan. de 2020abr. de 2023jul. de 2026
ArtefatosPackagistcomposer require codelytv/php-ddd-example
README

Codely logo

🐘🎯 Hexagonal Architecture, DDD & CQRS in PHP

Codely Open Source projects CodelyTV Courses Symfony 7 Type Coverage CI pipeline status

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

Take a look, play and have fun with this. Stars are welcome 😊

View Demo · Report a bug · Request a feature

🚀 Environment Setup

🐳 Needed tools

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

🛠️ Environment configuration

  1. Create a local environment file (cp .env .env.local) if you want to modify any parameter

🔥 Application execution

  1. Install all the dependencies and bring up the project with Docker executing: make build
  2. Then you'll have 3 apps available (2 APIs and 1 Frontend):
    1. Mooc Backend: http://localhost:8030/health-check
    2. Backoffice Backend: http://localhost:8040/health-check
    3. Backoffice Frontend: http://localhost:8041/health-check

✅ Tests execution

  1. Install the dependencies if you haven't done it previously: make deps
  2. Execute PHPUnit and Behat tests: make test

👩‍💻 Project explanation

This project tries to be a MOOC (Massive Open Online Course) platform. It's decoupled from any framework, but it has some Symfony and Laravel implementations.

⛱️ Bounded Contexts

  • Mooc: Place to look in if you wanna see some code 🙂. Massive Open Online Courses public platform with users, videos, notifications, and so on.
  • Backoffice: Here you'll find the use cases needed by the Customer Support department in order to manage users, courses, videos, and so on.

🎯 Hexagonal Architecture

This repository follows the Hexagonal Architecture pattern. Also, it's structured using modules. With this, we can see that the current structure of a Bounded Context is:

$ tree -L 4 src

src
|-- Mooc // Company subdomain / Bounded Context: Features related to one of the company business lines / products
|   `-- Videos // Some Module inside the Mooc context
|       |-- Application
|       |   |-- Create // Inside the application layer all is structured by actions
|       |   |   |-- CreateVideoCommand.php
|       |   |   |-- CreateVideoCommandHandler.php
|       |   |   `-- VideoCreator.php
|       |   |-- Find
|       |   |-- Trim
|       |   `-- Update
|       |-- Domain
|       |   |-- Video.php // The Aggregate of the Module
|       |   |-- VideoCreatedDomainEvent.php // A Domain Event
|       |   |-- VideoFinder.php
|       |   |-- VideoId.php
|       |   |-- VideoNotFound.php
|       |   |-- VideoRepository.php // The `Interface` of the repository is inside Domain
|       |   |-- VideoTitle.php
|       |   |-- VideoType.php
|       |   |-- VideoUrl.php
|       |   `-- Videos.php // A collection of our Aggregate
|       `-- Infrastructure // The infrastructure of our module
|           |-- DependencyInjection
|           `-- Persistence
|               `--MySqlVideoRepository.php // An implementation of the repository
`-- Shared // Shared Kernel: Common infrastructure and domain shared between the different Bounded Contexts
    |-- Domain
    `-- Infrastructure

Repository pattern

Our repositories try to be as simple as possible usually only containing 2 methods search and save. If we need some query with more filters we use the Specification pattern also known as Criteria pattern. So we add a searchByCriteria method.

You can see an example here and its implementation here.

Aggregates

You can see an example of an aggregate here. All aggregates should extend the AggregateRoot.

Command Bus

There is 1 implementations of the command bus.

  1. Sync using the Symfony Message Bus.

Query Bus

The Query Bus uses the Symfony Message Bus.

Event Bus

The Event Bus uses the Symfony Message Bus. The MySql Bus uses a MySql+Pulling as a bus. The RabbitMQ Bus uses RabbitMQ C extension.

📱 Monitoring

Every time a domain event is published it's exported to Prometheus. You can access to the Prometheus panel here.

🤔 Contributing

There are some things missing (add swagger, improve documentation...), feel free to add this if you want! If you want some guidelines feel free to contact us :)

🤩 Extra

This code was shown in the From framework coupled code to #microservices through #DDD talk and doubts where answered in the DDD y CQRS: Preguntas Frecuentes video.

🎥 Used in the CodelyTV Pro courses:

🌐 remember to visit our courses

Repositórios relacionados
kgrzybek/modular-monolith-with-ddd

Full Modular Monolith application with Domain-Driven Design approach.

C#MIT Licensedddddd-architecture
13.9k2.2k
ThreeDotsLabs/watermill

Building event-driven applications the easy way in Go.

GoGo ModulesMIT Licensewatermillevent-driven
watermill.io
9.8k500
redpanda-data/connect

Fancy stream processing made operationally mundane

GoGo Modulesmessage-queuestream-processing
docs.redpanda.com/connect/home/
8.7k951
EduardoPires/EquinoxProject

Web Application ASP.NET 9 using Clean Architecture, DDD, CQRS, Event Sourcing and a lot of good practices

C#MIT Licenseequinoxcqrs
equinoxproject.azurewebsites.net
6.8k1.7k
ThreeDotsLabs/wild-workouts-go-ddd-example

Go DDD example application. Complete project to show how to apply DDD, Clean Architecture, and CQRS by practical refactoring.

GoGo ModulesMIT Licensegogolang
threedots.tech
6.4k583
kurrent-io/KurrentDB

KurrentDB is a database that's engineered for modern software applications and event-driven architectures. Its event-native design simplifies data modeling and preserves data integrity while the integrated streaming engine solves distributed messaging challenges and ensures data consistency.

C#Othercqrseventsourcing
kurrent.io
5.8k678
EventStore/EventStore

EventStoreDB, the event-native database. Designed for Event Sourcing, Event-Driven, and Microservices architectures

C#Othercqrseventsourcing
eventstore.com
5.4k659
oskardudycz/EventSourcing.NetCore

Examples and Tutorials of Event Sourcing in .NET

C#Creative Commons Attribution Share Alike 4.0 Internationalevent-sourcingcqrs
event-driven.io
3.7k545
AxonIQ/AxonFramework

Framework for Evolutionary Message-Driven Microservices on the JVM

JavaMavenApache License 2.0javacqrs
axoniq.io
3.6k854
ngxs/store

🚀 NGXS - State Management for Angular

TypeScriptnpmMIT Licensecqrsredux
ngxs.io
3.5k404
AxonFramework/AxonFramework

Framework for Evolutionary Message-Driven Microservices on the JVM

JavaMavenApache License 2.0javacqrs
axoniq.io
3.4k798
kgrzybek/sample-dotnet-core-cqrs-api

Sample .NET Core REST API CQRS implementation with raw SQL and DDD using Clean Architecture.

C#MIT Licensedddddd-example
kamilgrzybek.com/design/simple-cqrs-implementation-with-raw-sql-and-ddd/
3.1k676