Discover more from Víctor Falcón
Three Keys to Create a maintainable Laravel Application
These are all my learnings after been working and building more than 25 laravel projects.
Laravel is a framework that gives you a lot of freedom. It allows you to structure your application code as you wish.
You can decide to put all the code in the controller, you can create use cases, DDD, or use jobs to host all your logic.
Thanks to developerjoy.co I have been able to work on dozens of different projects and I have seen first-hand what makes a Laravel application easy to maintain as it grows.
For me, if I had to keep only three keys, they would be the following:
Table Of Contents
Use Events and Listeners
It is very common, in all applications, to have to do something extra after a simple use case.
For example, send an email, after creating a user.
This type of case tends to dirty our use cases and forces us to add more and more logic. In addition, we make the use case slower and slower, and the user has to wait longer.
Why does the user have to wait, when they register, for us to send an email?, for example. Has no sense.
To avoid this and keep our use cases clean, you should use events and listeners.
UserCreated event and a
Every time you need to do something else in a use case, you will simply have to create the listener. Your use case will be able to continue working as is, without any changes.
Tests everything, it's super important
I know, making tests is boring and a waste of time. Really?
Apart from making sure that the basic use cases still work, it will also help you keep your code clean. You do not believe me?
If you test your use cases you will be able to refactor the code when and how you want without breaking anything. This is something of a superpower.
Do you want to simplify your use case and start launching events? Do you want to move your site code? No problem. First write a test and then you can play what you want with the assurance that you are not breaking anything.
Plus, testing in Laravel is easy.
Most of the time, testing an endpoint is just a couple of lines. It's easy to make and quick to execute.
Really, there is no turning back. When you prove the value that the tests give you, you will not stop writing them.
Embrace DDD and create Modules
When an application with Laravel grows we find many models, controllers, events, listeners and jobs together in the same folder.
This makes it impossible to have a context of what is happening with our model.
Imagine that you are working with the user, and you need to update a use case, the registration case, for example.
By having everything together it is impossible, at a single glance, to see everything that affects our user model, in which controllers it is being used and what events it launches.
Using DDD, you will separate your code into modules and be able to see at a glance what you are working on.
Personally I like to create a
src folder and there create a Module for each model or entity.
In the end it would be like this:
/src /Users User.php /Listeners /Controllers /Events /Providers ... /Posts Post.php ... ...
Believe me, in the end, as your application grows, you will appreciate it.