Is there a practice of using dot files to store config info and constants in php? If so, how do I parse them? If not, then what would be a good way to isolate and store sensitive info like db passwords, or other config info?
Is there a practice of using dot files to store config info and constants in php? If so, how do I parse them? If not, then what would be a good way to isolate and store sensitive info like db passwords, or other config info?
Top comments (6)
Yes, there is! Some frameworks like Laravel and Symfony have
.env filesupport 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
.envfile 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
.htconfig.ini
index.php
Wow! thanks, I looked into this. It looks better than using normal dot files since there is a built in function to read ini files. Thanks a lot
Don't forget add to .gitignore(if you use git) this dot files !
Yes yes...always :D