Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use dotenv library instead of parsing ini file?

Tags:

php

ini

phpdotenv

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?

like image 885
Lijiebin Avatar asked Dec 15 '15 10:12

Lijiebin


People also ask

What is. env file PHP?

An . env file is a plain text file which contains environment variables definitions which are designed so your PHP application will parse them, bypassing the Apache, NGINX and PHP-FPM. The usage of . env files is popular in many PHP frameworks such as Laravel which has built-in support for parsing .

What is Dotenv PHP?

PHP dotenv has built in validation functionality, including for enforcing the presence of an environment variable. This is particularly useful to let people know any explicit required variables that your app will not work without.

What is use of .ENV file in laravel?

Laravel's default .env file contains some common configuration values that may differ based on whether your application is running locally or on a production web server. These values are then retrieved from various Laravel configuration files within the config directory using Laravel's env function.

How do I create an .ENV file?

Once you have opened the folder, click on the Explorer icon on the top left corner of the VSCode (or press Ctrl+Shift+E) to open the explorer panel. In the explorer panel, click on the New File button as shown in the following screenshot: Then simply type in the new file name . env ...


2 Answers

It worked for me using Laravel .env

<?php
var_dump(parse_ini_file('.env', false, INI_SCANNER_RAW));
like image 135
Érycson Nóbrega Avatar answered Oct 22 '22 00:10

Érycson Nóbrega


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.

like image 25
Sergey Chizhik Avatar answered Oct 22 '22 02:10

Sergey Chizhik