implement DDD in MVC Frameworks - PHP implement DDD in MVC Frameworks - PHP laravel laravel

implement DDD in MVC Frameworks - PHP


in mvc the model is a layer and it's contain all the domain business logic.

I doubt MVC pattern itself declares something special about the Domain. It operates model as a bag of properties and doesn't care how it was created and how it guards its invariants.

At the same time Onion architecture states that it's important to isolate Domain out of Application Service (which MVC Framework is). So I like to place Domain layer which contains Entities, Value objects, Domain events and Aggregates to a separate module or a top-level folder.

enter image description here

One more reason for placing Domain separately from MVC stuff is that it will allow you easier manage multiple bounded contexts, because each context needs its own module/folder.

I sugget you to check out this ASP MVC project structure. It was designed by well-known DDD expert. Besides domain, please take a look at how MVC part is organized. It exploits feature slice approach which is getting more and more popular these days and I find it extremely useful.


Although I'm quite new to the world of DDD, in the process of gradually migrating an application I was working on to a more DDD-oriented structure I also confronted the question of directory structure. Piecing together the information I was able to find which wasn't entirely conceptual I came up with the following simplified directory structure, (which exists within a CRUD-oriented Laravel application), that has served me well enough:

app/    ProjectName/        Core/            Application/            Domain/            Infrastructure/        User/            Application/                Services/                    CreateUserService.php                    FindUserService.php                    UpdateUserService.php            Domain/                Role.php                RoleDAO.php                User.php                UserDAO.php                UserNotCreated.php                UserNotFound.php                UserNotUpdated.php                UserWasCreated.php                UserWasUpdated.php            Infrastructure/                EloquentRoleDAO.php                EloquentUserDAO.php

Addressing your specific concerns, repository interfaces and entities were placed within Domain folders underneath each separable component of the application (e.g. - User). Additionally, this is where I placed any Domain events and exceptions. Repository implementations were placed under each Infrastructure folder. Application services are placed within a Services directory under Application directories.

Leaving aside the hybrid nature of my own application (I'm using ORM-reliant DAO's/Entities, transaction scripts, and avoiding Value Objects just to name a few diversions), this may still help serve as a rough idea of a potential DDD directory structure within an MVC app.


I am agree with @Zharro's suggestion.

Good structure is like below:

  • View (Contains only view part like twig, html and assets)
  • Core (Controller, Form, Listeners, Helpers)
  • BusinessLogic (Include services)
  • Entity (Entity, Commands, Validator)

1) View part access only CoreBundle and does not change behaviour of database.

2) Core part access BuisnessLogic and Entity

3) BuisnessLogic part access only Entity

4) Entity part access only database.