How to Structure Laravel Project
Where to put your logic?
The most often question I hear in Laravel is this:
“Where should I put this code logic?”
Here’s a typical post from StackOverflow:
Basic Laravel is an MVC framework, it’s clear what should be in Model, View and Controller. But what if you have custom logic for processing data before passing it to the view? Where should it be?
- In the same Controller method?
- Separate private Controller method?
- Static function in Model?
- Separate class, like Service?
- Or, maybe, global helper function without any class?
- Also, I’ve heard Repository is a widely-used pattern?
And the list goes on.
Of course, the correct answer is it depends. But depends on what?
This is what I’m gonna talk about in this course.
The goal is to discuss practical examples of structuring larger Laravel applications, various approaches and patterns. We will analyze existing popular open-source projects and their structure.
Important notice: this course won’t be about design patterns. So fancy keywords like Singleton, Factory, Adapter, Repository etc will be used only when appropriate.
What I will try to do is focus on the practical side: folders, classes and methods, and not on how that pattern actually is called in theory. In reality, every project is actually a mix of patterns, and developers don’t even know how they’re called.
I consider myself a Laravel expert. Work with Laravel around 6 years, I also write about it on my Laravel Daily Blog
Active contributor to official Laravel News blog, latest article: 20 Laravel Eloquent Tips and Tricks
Also sharing my thoughts on YouTube channel "Laravel Business": https://www.youtube.com/channel/UCTuplgOBi6tJIlesI...
PreviewUse More Eloquent: Accessors/Mutators, Scopes, Observers & "Magic" Methods (10:09)
PreviewControllers and Routes: Where to Put "Extra" Methods? (8:53)
StartStructuring Blade Files: @extends, @yield and Other Commands (13:17)
StartCustom Base Controllers and Base Models (5:13)
StartConfiguration: Never Use env() Directly in Controllers/Models (5:26)
StartForm Request Classes for Validation (4:35)
StartUser Notifications: Stop Using Mail::send() (5:50)
PreviewJobs, Queues and Scheduling Tasks (5:04)
StartEvents and Their Listeners (7:15)
StartAPI Resources for Data Transformation (9:17)
StartGlobal Helpers, External PHP Classes, Autoloading and PSR-4 (6:52)