We are always in a rush and wanting to develop as quickly as possible, and we often end up adopting old habits and building old software, an item that we can improve a lot is this little thing called environment, let's understand a little more about that.
First of all, I would like to show here the focus on the concept of configurations for laravel, I will not worry about the remaining patterns, such as resources or others like that.
1 - Let's seek knowledge!
A while ago I heard that I couldn't do this ENV pass within my crazy codes, I was like; wow
So the purpose is to understand the reasons, because this way, we can make better decisions as engineers.
1.1 - Correct way or a point of view?
So come on little future master, let's put a value in an environment variable so we can play around, so let's call it:
Let's go to the first point, great decision to place this configuration as an environment variable, this way you make everything easier to manage different environments (Production, approval, If you have one? ) and much safer, because this value It won't be exposed when you do that naughty git push in your repository (git/bitbucket), can you imagine your water secret being leaked on the internet, how sad.
And to retrieve this value in Laravel we can use the env() method or also use another package such as Support from Illuminate (it reminds me of minions, I don't know why? )
env: Laravel Helper ( Which uses Env::get )
Env::get: Env class from the Support Illuminate package
Wow, it's ready, so why keep inventing fashion? The truth is that this is not a good idea, I will explain why soon, stay with me.
For the different solution, we can mention the configuration files that will retrieve the value of the environment created, so the configuration file is centralized and come on, seriously! much better for reading code.
In this specific situation you can already use the existing config file, called services.php, but nothing prevents you from creating a file for your specific context.
In the path config/services.php
And so we will call it in the code as follows:
2 - Cool, but why should I use configs to retrieve variables and not just fetch them directly?
I'll try to show some reasons:
2.1 - Increased performance and better use of IO
Think about the following situation, do you have files in production that will be accessed all the time, carrying out considerable IO, which will increase machine resources and depending on the situation, system slowdowns? (And believe me, until you actually find the problem, you will go through things that will question all your knowledge)
When we talk about the production environment, it is recommended that you cache your beautiful configuration files, with Laravel you can use artisan
php artisan config:cache
This beautiful command takes all the configuration files and their respective values and compiles them into a single php file, which increases performance. This is due to the fact that the IO numbers are reduced to 1
Curiosity: When you use this command, env() starts to return NULL because it disables this function. So if from "Neida" a env() yours stopped working, that's it.
Env() Perform IO operations and they are expensive and slow.
It is best to do this operation once at the beginning of the application rather than having to perform the operation every time you need an env.
2.2 - Organization and standardization in the best possible way
You can easily keep all your settings as you want. You can put the name of the dog you want, create a folder and structure all the arrays as you want too, this will make the structure cleaner (please use in moderation)
For example:
Look at this key:
and now look at this key:
In my humble opinion the 1st is much better.
And besides, you can have your similar configurations in the correct and centralized place without having to place them "randomly" in your code, and I really like this idea of structuring the array that makes total sense for the application.
And guidance for new developers is much simpler, as you can tell them where and how to make a new configuration.
Keep it simple, be the difference!
Thank you for everything so far.
Source:
Laravel 11 Documentation Configuration
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3