How to read the value from the .env file PHP?
Reading value from environment file (.env) Php
This is how it started,
In PHP application development Laravel framework most of the features are built-in. There are times PHP environment variables stores the most important values like application keys and configuration settings, and to read in other section of the application.
You can access any variable value from .env using $_SERVER or $_ENV arrays. In manual PHP manual used to getenv — Gets the value of an environment variable.
How does Php manual help in application development?
Most probably, if you are a PHP application developer might know sometimes it is hard to get what you want from a manual description.
If you are reading this remember I was in your position before.
You may have a question, how do I read or get value from the environment or read from the .env file, get variables from .env file php, PHP Get environment variable, php getenv not working, Environment variables, getenv all these could be sorted out.
PHP environment variables allow your scripts to glean certain types of data dynamically from the server.
Can I read the value from the .env file?
Yes, the question is obvious. But a security concern it is not recommended to read from the environment file without placing a middle man.
From the first image describes, app.php is the middle guy that retrieves value and passes it to other parts of the code controller, command, etc.
If you try to search google, you may find StackOverflow provides a solution to read directly from .env, this is what will happen when you execute your value becomes null though the environment file has value.
Let's get started,
All the source codes below you find are the working code and sharing with you.
How I resolved step by step?
Before that this happened with my team task discussions, a change requested to add a new feature to an existing project that must send email notification (will share in another article sending email in outlook step by step.) to user configured.
The PHP application environment file holds most of the email settings, SMTP host configured.
Step 1 : Configure to set custom value in PHP .env file
Note that the .env file has two variables configured as below.
MAIL_FROM_ADDRESS=arjun.premier@outlook.com
Mail_TO=Administrator
Step 2 : In App.php read from using env('') method
The diagram shows a simple way to access .env value
mail_from=> env('MAIL_FROM_ADDRESS',false)
Step 3: In Command PHP use config class to read from app.php
First, make sure you add a config class in the namespace
use Config;
Second, the most important is to add a keyword app in the config class,
This is how it is done
config('app.mail_from');
If you notice the image circled, the config method has it all.
Summary
To summarize the first step is to define value in the .environment file.
Secondly, you need to have a middleman which is app.php reads from .env
Finally, wherever to consume the value include namespace use config;
And use config('app. keyword') to get the value defined in .env file
With the above three steps explained, this is a simple way to configure in the Php application environment(.env) file and access any part of the application.
0 Comments