Yes, there is! Some frameworks like Laravel and Symfony have .env file support out of the box.
.env file
In your project, you could use vlucas/phpdotenv, which does exactly what you want and it's pretty simple to implement, check it out:
1 - Install it using Composer by typing the following command on your terminal:
composer require vlucas/phpdotenv
2 - Now let's configure it on your application's starting point (I'll assume it is index.php):
index.php
<?php // index.php include 'vendor/autoload.php'; $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); // your code goes here...
3 - Cool, we're almost there! Now create the .env file at your project's root directory and populate it with your configs like this:
.env
DB_USER="user" DB_PASSWORD="ultraSecurePassword" DB_NAME="awesome_db" FOO="bar"
4 - Finally, to retrieve the env values:
<?php // db.php (naming things is hard) $user = env('DB_USER'); // OR $user = getenv('DB_USER'); // OR $user = $_ENV['DB_USER']; // OR EVEN $user = $_SERVER['DB_USER'];
Hope it helps! ;) (if there's any typo, feel free to correct me, i'll appreciate)
Awesome! Everything I was looking for!!! Thanks a bunch
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yes, there is! Some frameworks like Laravel and Symfony have
.env file
support out of the box.In your project, you could use vlucas/phpdotenv, which does exactly what you want and it's pretty simple to implement, check it out:
1 - Install it using Composer by typing the following command on your terminal:
2 - Now let's configure it on your application's starting point (I'll assume it is
index.php
):3 - Cool, we're almost there! Now create the
.env
file at your project's root directory and populate it with your configs like this:4 - Finally, to retrieve the env values:
Hope it helps! ;)
(if there's any typo, feel free to correct me, i'll appreciate)
Awesome! Everything I was looking for!!! Thanks a bunch