Why use dotenv library instead of parsing ini file?
Asked Answered
J

3

12

Within PHP, *.ini files can be read by using parse_ini_file(). However, various frameworks (Laravel included) opt to, instead, bring in a separate library to parse an environment file.

What is the reasoning behind using this "dotenv" solution instead of ini files and the built-in PHP function?

Jadejaded answered 15/12, 2015 at 10:42 Comment(0)
W
2

It's a good question. I've found a few mentions on php.net (search by the keyword parse_ini_file). The main problem, I suppose, is that parse_ini_file doesn't support some features, such as constants, expressions, etc. Also, I guess, some developers would like to perform such operations in OOP style.

Witless answered 15/12, 2015 at 11:26 Comment(1)
I really appreciate of you! Also, ini parser has some disadvantage, however there are many advantages, right? So, i still understand why dotenv, hehe. : )Jadejaded
I
6

You can parse a .env file natively in PHP using parse_ini_file. This worked for me using Laravel .env

<?php
var_dump(parse_ini_file('.env', false, INI_SCANNER_RAW));
Isidore answered 6/12, 2017 at 15:5 Comment(3)
What exactly are you trying to say?Amber
Thanks, tried it and this puts the .env file into a PHP array for accessUnobtrusive
This does not answer OP's question. It's simply restating that it is possible to use parse_ini_file, which was already mentioned in the original question.Handicraft
W
2

It's a good question. I've found a few mentions on php.net (search by the keyword parse_ini_file). The main problem, I suppose, is that parse_ini_file doesn't support some features, such as constants, expressions, etc. Also, I guess, some developers would like to perform such operations in OOP style.

Witless answered 15/12, 2015 at 11:26 Comment(1)
I really appreciate of you! Also, ini parser has some disadvantage, however there are many advantages, right? So, i still understand why dotenv, hehe. : )Jadejaded
A
1

We can access .env variables in many ways, but it is not a benefit for single project. It is better to use unified way to access these variables throughout the project any way. However, laravel is used for many projects and opting for flexibility is understandable for them.

Amber answered 5/2, 2018 at 23:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.