Domain-Driven Design Approach in Laravel

Zainal Hasan |

Domain-Driven Design is one of many approaches for developing software with a high complexity core business process. But, not all developer team must use this approach. Yes, I mean this is not completely needed each developing software, but this can help the developer team to develop and maintain the software more easily.


Each framework has each directory structure and architecture concept. Laravel Framework implements the MVC design pattern. But, Laravel is very flexible, so the developer can implement the DDD design approach in this framework easily (with each pro and cons). The DDD approach has three focuses:

  1. Focus on the core domain and domain logic
  2. Base complex designs on models of the domain
  3. Constantly collaborate with domain experts to improve the application model and resolve any emerging domain related issues

An example of DDD directory structure

Example of directory structure

In Laravel, I recommend to register the namespace to autoload class of your app, so you can easily call it in another part of your code.

Register autoload namespace
Register autoload namespace

This is an example of Eloquent model and Services implementation code

Event Entity model
Event Entity model
Attempt Event service
Attempt Event service

After that, you can access it like this:

API Controller for event attempt
API Controller for event attempt

As you can see, each service or anything must never return a response, view, or HTTP response. Instead, always return a primitive data type (boolean, integer, string, array, object) or throw an exception. So, in the controller, we can catch and return a response.

Then, each your business grows up and needs more feature enhancement, you can easily manage it on the domain 😎. Also, if you want to implement the microservice architecture, you only need to make a bit change.

View piece of the source code at my gist (https://gist.github.com/zhanang19)

Ps: directory structure of the example in this article may be different from the other reference, but it’s also a Domain-Driven Design Approach

Reference

Related post