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?

8
  • Your diagram already shows where to put the business logic (more or less). Commented Jan 4, 2020 at 21:00
  • I mean in Laravel where should I add that, means shall i create BL directory and and some classes, etc.? Commented Jan 4, 2020 at 21:01
  • Sounds reasonable to me. Commented Jan 4, 2020 at 21:02
  • Hence wanted an opinion on what is the best practice we can use in Laravel, or is there anything Laravel provides OOB to support this. Commented Jan 4, 2020 at 21:03
  • Asking for "best practices" is not the best way to spend your time here. The "best practice" is the one that most effectively meets your specific requirements. Ask us about a specific problem you are having. Commented Jan 4, 2020 at 21:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.