https://life-long-bunny.fra1.digitaloceanspaces.com/media-library/production/56/conversions/programmer_v_02_o9k1tl-optimized.jpg
3 crucial Laravel architecture best practices for 2023
Updated on
Table of contents:
How should you organize your Laravel app to best serve your needs? Well, the good news is that you don’t have to worry about this since you are using a framework! Stick to the defaults unless you have good and objective reasons to do otherwise.
And yet, people can’t stop overthinking the architecture of their projects.
To me, it seems that the urge to deviate from the standard project structure often reveals a deeper issue – a fundamental inability to maintain organization. Whether you adhere to the Laravel architecture or significantly modify it, the outcome is likely to be disorganized.
Therefore, to address this problem, we will put ourselves in shoes that would fit almost any enterprise project.
Before we begin, though, let’s define what an “enterprise project” is in our context. Essentially, it’s a public facing project with lots of users that generates revenue, making it vital to continuously evolve by adapting to new technologies, business requirements, and market trends.
Here’s what is expected from the team of such projects:
- Easy collaboration.
- Maximize compatibility with third-party solutions that will help maintain the cost of development down.
- Keep the cost of onboarding low. To achieve this, new hires need to easily find their way around the codebase, which can make them somewhat productive even when they lack domain knowledge.
With these goals in mind, let’s dive into what I, and most of the experts from the community, think are the best architecture practices.
Using Laravel is meant to make your life easier, not harder.
- First, following conventions helps ensure that new hires can quickly find everything they need and start being productive as soon as possible. Laravel is a popular framework, and most developers will already be familiar with its default folder structure. By sticking to this, you help minimize the learning curve for new team members.
- Also, a profitable project is supposed to last for many years. People come and go. You will likely move on to something else. Why wouldn’t you make it easy for the ones who will take over?
- Additionally, by following the framework’s defaults, you ensure compatibility with many first and third-party packages. This can be crucial for maintaining development costs down and maximizing the use of available resources.
While it’s essential to keep the default folder structure, it’s also necessary to organize your code in a way that makes sense for your project. One way to do this is by organizing it by domain, without breaking the default folder structure.
This means that, for example, inside your Models folder, you could create a Blog folder. This way, when using the php artisan make:model Blog/Category
command, the new file will be created at the right place.
This approach can also be used for controllers, middlewares, policies, and so on. Organizing your code the intended way will help you maintain a compatible, clean and intuitive codebase.
Developers love discovering new ways of doing things, and it’s always tempting to experiment with new packages or approaches. This is fine for personal projects or when you are working alone, but it may not be ideal in a team setting.
When you hire Laravel developers, you are hiring them to expand and maintain your product using Laravel. It’s essential to remember this and stick to the built-in features of Laravel whenever possible.
For example, don’t use Data Transfer Objects (DTOs) instead of custom form requests unless there are good and objective reasons to do so. Using the built-in features of Laravel ensures that all developers on your team are working with the same set of tools and reduces the learning curve for new hires.
Matt Stauffer, who has a lot of experience building apps for enterprise as the CEO of Tighten, talks about how keeping things simple benefits big projects.
James Brooks is a core Laravel team member. He knows what working with a big team and a big codebase are. He also asked me to include it in this article, so there he is!
https://twitter.com/jbrooksuk/status/1697182125663945015?s=20
Sebastian Schlein is the co-founder of BeyondCo, a company deeply involved with Laravel, and he also thinks that you should stick to the framework’s defaults. This is a tweet from 2019 by the way.
https://twitter.com/seb_sebsn/status/1186228940555345921
Jason McCreary, from Laravel Shift, also showcases his favorite way of organizing Laravel projects. Looks familiar, don’t you think?
https://twitter.com/gonedark/status/1333474208123412488
All that being said, at the end, results matter the most. Here’s a tweet from Taylor Otwell himself about keeping an open mind:
Laravel News Links