3

I am new to Laravel and I see MVC pattern here, I would like to know the best practice to active N-tier architecture in Laravel.

For example: Controller, Business Logic, Data Access layer (assuming Model in Laravel), Presentation Layer (DTO's used as JSON response)

enter image description here

Where do I fit Business Logic and DTO's in Laravel?

Sharad Soni
  • 378
  • 1
  • 5
  • 18

1 Answers1

1

The business logic might be in many places on Laravel:

  1. In the controller itself
  2. In Events and Event Listeners
  3. In Services
  4. In Jobs (queued or not)

There many ways to do it, it's up to you decide which fits to each case. If you try following SOLID Principles you will fell the necessity to use some of this options.

For the presentation layer, you can use Laravel API Resources, since it provides a simple way to transform your data and output as JSON in a standardized format.

Elias Soares
  • 9,884
  • 4
  • 29
  • 59
  • Thanks for your help. My ideology is to not write logic in controllers. I think, Logic in Events might not fit my requirements. I will try out Services though Also, jobs are useful but not the place where I am looking for, Have read about API resources, will try it out that too. So services and API resources is what I am thinking now. – Sharad Soni Jan 04 '20 at 21:21