Symfony and 

Hexagonal Architecture
Software Developer, father, geek, PHP and
Node.js expert. Open-source enthusiast,
active contributor on GitHub and
StackOverflow.
Who am I?
Alessandro Minoccheri
Software developer @Flowing
@minompi
@AlessandroMinoccheri
◆
A real story
◆
◆
How do you usually start a new Symfony Project
• Install Symfony skeleton project
• Remove demo code
• Autogenerate entities
• Start coding from controllers or autogenerate those
• Ready to develop the application
Hexagonal architecture
6 months later…
◆
◆
How many points inside the code
do you need to change to replace
that service?
◆
42 different places in all over the
code-base.
◆
class PaymentController
{
public function payAction(Request $request, YourBankGateway $gateway)
{
$gateway->pay($request->get('amount'), false, false, 0, 'EUR');
}
}
class Payment
{
public function pay(Request $request)
{
$gateway = new YourBankGateway();
$gateway->pay($request->get('amount'), false, false, 0);
}
}
class User
{
public function pay(Request $request)
{
$gateway = new YourBankGateway();
$gateway->pay($request->get('amount'));
}
}
Why does this situation exists?
◆
Problems when you are guided from the framework
• Impossible to upgrade framework and vendors.
• Cost of maintenance
• Developers are not motivated because the stack it's very old for the
reason of point 1.
• Not maintainable application because everything is coupled.
• A lot of technical debt
• Impossible to change implementation easily.
So, what really matters in
software?
◆
 What really brings value in
software is the solution to the
specific problem in the domain
◆
Hexagonal Architecture history
◆
The hexagonal architecture divides a system into several
loosely-coupled interchangeable components, such as the
application core, the database, the user interface, test
scripts, and interfaces with other systems. 

This approach is an alternative to the traditional layered
architecture. (Wikipedia)
◆
Is it an over-engineer strategy?
◆
Coupled code
namespace AppService;
use SymfonyComponentHttpFoundationRequest;
class Payment
{
public function doPayment(Request $request)
{
$gateway = new YourBankGateway();
$gateway->pay($request->get('amount'), false, false, 0);
}
}
Decoupled code
interface GatewayProvider
{
public function pay(Money $amount): void;
}
Decoupled code
use AppValueObjectMoney;
class YourBankGateway implements GatewayProvider {
public function pay(Money $amount): void
{
//do stuff..
}
}
Decoupled code
class Payment
{
/**
* @var GatewayProvider
*/
private GatewayProvider $gatewayProvider;
public function __construct(GatewayProvider $gatewayProvider)
{
$this->gatewayProvider = $gatewayProvider;
}
public function doPayment(Money $amount)
{
$this->gatewayProvider->pay($amount);
}
}
Why is it important to decouple
your code?
◆
Maintainable application
• Changes in one part of an application should affect as few other
places as possible
• Adding features shouldn't require to touch any part of the code-
base
• Adding new ways to interact with the application should require as
few changes as possible
• Debugging should require as few workarounds
• Testing should be relatively easy
Single responsibility for
architecture:

What changes for the same reason,
should be grouped together
◆
Examples
• Code related to the framework
• Code related to the specific service
• Code related to the domain
The important distinction from domain and infrastructure
Domain
• Models: entities, value object or others
• interfaces for boundary objects
Infrastructure
• framework
• implementations for boundary objects
• Web controllers, CLI commands
Application
• Use cases
Why is it so important to have
maintainable software?
◆
Why an Hexagon?
Ports
◆
Ports are like contracts
They will not have any representation in the codebase.
There is a port for every way a use case of the application can be
invoked .
There are input and output ports.
Input ports
interface PayOrderHandler
{
public function handle(PayOrderCommand $command): void;
}
Output ports
interface UserRepository
{
public function save(User $user): void;
public function findAll(): UserCollection;
}
Adapters
◆
Adapters definition
Adapters are the implementation of the ports because for each of
these abstract ports we need some code to make the connection
work.
Adapter example
class MysqlUserRepository implements UserRepository
{
/**
* @var Connection
*/
private $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public function save(User $user): void
{//do stuff...}
public function findById(UserId $userId): User
{//do stuff...}
public function findAll(): UserCollection
{//do stuff...}
}
Hexagonal architecture
Directories structure example
Src
Payment
Domain

Application
Infrastructure
Cart
Domain
Application
Infrastructure
Dependency inversion principle
Advantages of using hexagonal
architecture
◆
increase testability
1
More unit tests
2
You can replace an implementation without affecting the domain
3
You can postpone the choice of 

vendors, databases, servers, etc...
4
you can update vendors and framework without touching your
domain code.
5
So you can create maintainable application
6
EXAMPLE
namespace AppUserInfrastructureController;
use AppUserApplicationCreateUserService;
use AppUserApplicationDTOCreateUserRequest;
use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyComponentHttpFoundationRequest;
class UserController
{
public function saveAction(Request $request, CreateUserService $createUserService)
{
$createUserRequest = CreateUserRequest::create($request->request->all());
$createUserService->createUser($createUserRequest);
return new JsonResponse();
}
}
namespace AppUserApplication;
use …
class CreateUserService
{
private UserRepository $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function createUser(CreateUserRequest $createUserRequest): CreateUserResponse
{
$user = User::create(
$createUserRequest->getName(),
$createUserRequest->getPassword()
);
$this->userRepository->save($user);
return CreateUserResponse::createFromUser($user);
}
}
When to use it?
• When you have a new project
• When you need to develop a new context into a legacy application
• always? When to use it
How about legacy code without
hexagonal architecture?
◆
Golden rule
Boy scout rule:
Leave your code better than you found it.
When to use it
Where can you start from?
• Using more interfaces
• Using dependency injection
• Practice with kata projects
• Apply the architecture into a real project
When to use it
Next improvements
- DDD: Domain driven design
- CQRS pattern (Command Query Responsibility Segregation)
- Event sourcing
- TDD
- BDD
When to use it
We are hiring!
Thank you!
@minompi https://joind.in/talk/f6069

More Related Content

PDF
How Symfony Changed My Life
PPTX
Zend Studio Tips and Tricks
PDF
The Naked Bundle - Symfony Live London 2014
PDF
Tech friday 22.01.2016
ODP
Dependency Injection, Zend Framework and Symfony Container
PDF
Advanced JavaScript - Internship Presentation - Week6
PDF
Create Your Own Framework by Fabien Potencier
PDF
Clean Architecture
How Symfony Changed My Life
Zend Studio Tips and Tricks
The Naked Bundle - Symfony Live London 2014
Tech friday 22.01.2016
Dependency Injection, Zend Framework and Symfony Container
Advanced JavaScript - Internship Presentation - Week6
Create Your Own Framework by Fabien Potencier
Clean Architecture

What's hot (20)

PDF
Angular Weekend
PDF
Conscious Decoupling - Lone Star PHP
KEY
IoC with PHP
PDF
Engineering Efficiency in LINE
PDF
TypeScript for Java Developers
PPT
C++basics
TXT
why c++11?
PDF
Beyond MVC: from Model to Domain
PDF
GOCON Autumn (Story of our own Monitoring Agent in golang)
PPTX
Node.JS error handling best practices
PPTX
Advanced Javascript
PDF
Brief Introduction on JavaScript - Internship Presentation - Week4
PDF
Angular genericforms2
PPTX
Get together on getting more out of typescript & angular 2
PDF
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
PDF
How AngularJS Embraced Traditional Design Patterns
PDF
Debugging and Profiling C++ Template Metaprograms
PDF
Angular performance improvments
PDF
Back to the future: Isomorphic javascript applications
PPTX
Getting started with typescript
Angular Weekend
Conscious Decoupling - Lone Star PHP
IoC with PHP
Engineering Efficiency in LINE
TypeScript for Java Developers
C++basics
why c++11?
Beyond MVC: from Model to Domain
GOCON Autumn (Story of our own Monitoring Agent in golang)
Node.JS error handling best practices
Advanced Javascript
Brief Introduction on JavaScript - Internship Presentation - Week4
Angular genericforms2
Get together on getting more out of typescript & angular 2
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
How AngularJS Embraced Traditional Design Patterns
Debugging and Profiling C++ Template Metaprograms
Angular performance improvments
Back to the future: Isomorphic javascript applications
Getting started with typescript
Ad

Similar to Hexagonal architecture (20)

PDF
Beyond MVC: from Model to Domain
PDF
So S.O.L.I.D Fu - Designing Better Code
PPTX
Reactive application using meteor
PDF
Hexagonal architecture in PHP
PDF
Complex Sites with Silex
PPTX
Practical AngularJS
PPTX
Creating your own framework on top of Symfony2 Components
PDF
PHP 8: Process & Fixing Insanity
PDF
What's New In Laravel 5
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
KEY
Intro to PSGI and Plack
PDF
Devfest 2023 - Service Weaver Introduction - Taipei.pdf
PDF
Using API Platform to build ticketing system #symfonycon
PPTX
Choose flutter
PDF
From framework coupled code to #microservices through #DDD /by @codelytv
PDF
First impression of the new cloud native programming language ballerina
KEY
Spring in the Cloud - using Spring with Cloud Foundry
PDF
WebNet Conference 2012 - Designing complex applications using html5 and knock...
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
PDF
Osiąganie mądrej architektury z Symfony2
Beyond MVC: from Model to Domain
So S.O.L.I.D Fu - Designing Better Code
Reactive application using meteor
Hexagonal architecture in PHP
Complex Sites with Silex
Practical AngularJS
Creating your own framework on top of Symfony2 Components
PHP 8: Process & Fixing Insanity
What's New In Laravel 5
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Intro to PSGI and Plack
Devfest 2023 - Service Weaver Introduction - Taipei.pdf
Using API Platform to build ticketing system #symfonycon
Choose flutter
From framework coupled code to #microservices through #DDD /by @codelytv
First impression of the new cloud native programming language ballerina
Spring in the Cloud - using Spring with Cloud Foundry
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Osiąganie mądrej architektury z Symfony2
Ad

More from Alessandro Minoccheri (9)

PDF
ServerSentEventsV2.pdf
PDF
ServerSentEvents.pdf
PDF
Kotlin hexagonal-architecture
PDF
Hexagonal architecture ita
PDF
Smart working
PDF
ServerSentEventsV2.pdf
ServerSentEvents.pdf
Kotlin hexagonal-architecture
Hexagonal architecture ita
Smart working

Recently uploaded (20)

PDF
Microsoft Office 365 Crack Download Free
PPTX
Human-Computer Interaction for Lecture 2
PDF
Workplace Software and Skills - OpenStax
DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PDF
E-Commerce Website Development Companyin india
PDF
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
PPTX
string python Python Strings: Literals, Slicing, Methods, Formatting, and Pra...
PPTX
ROI Analysis for Newspaper Industry with Odoo ERP
PPTX
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
PPTX
Computer Software - Technology and Livelihood Education
PPT
3.Software Design for software engineering
PPTX
Viber For Windows 25.7.1 Crack + Serial Keygen
PPTX
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
PPTX
R-Studio Crack Free Download 2025 Latest
PPTX
Plex Media Server 1.28.2.6151 With Crac5 2022 Free .
PDF
CapCut PRO for PC Crack New Download (Fully Activated 2025)
DOC
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
PDF
What Makes a Great Data Visualization Consulting Service.pdf
PDF
Website Design & Development_ Professional Web Design Services.pdf
PDF
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)
Microsoft Office 365 Crack Download Free
Human-Computer Interaction for Lecture 2
Workplace Software and Skills - OpenStax
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
E-Commerce Website Development Companyin india
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
string python Python Strings: Literals, Slicing, Methods, Formatting, and Pra...
ROI Analysis for Newspaper Industry with Odoo ERP
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
Computer Software - Technology and Livelihood Education
3.Software Design for software engineering
Viber For Windows 25.7.1 Crack + Serial Keygen
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
R-Studio Crack Free Download 2025 Latest
Plex Media Server 1.28.2.6151 With Crac5 2022 Free .
CapCut PRO for PC Crack New Download (Fully Activated 2025)
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
What Makes a Great Data Visualization Consulting Service.pdf
Website Design & Development_ Professional Web Design Services.pdf
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)

Hexagonal architecture