4

I am a beginner PHP , I found out that Laravel has .env file to save some configurations and php also have a file format .ini to save config.

I want to ask what is the difference in both, is one is better from another. Should i add dotenv in my core projects also or should i create class/function to access .ini for my config/environment variables.

i want to understand why dotenv is being created as we already had ini file extension system in php?

Puneet Singh
  • 3,477
  • 1
  • 26
  • 39
  • the .env is related to the current project, if you set variables in your .ini they will be the same for all your projects. Also depending on the kind of server you use, you may not have acces to the ini – jeremy castelli Oct 06 '15 at 16:52
  • Read this 1) https://github.com/vlucas/phpdotenv 2) https://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables – Nana Partykar Oct 06 '15 at 17:17
  • @jeremycastelli "Also depending on the kind of server you use, you may not have acces to the ini", can you please tell me which kind of server don't support ini, means servers which are hosting PHP but don't support ini .. i am curious .. – Puneet Singh Oct 06 '15 at 18:44
  • @PuneetSingh shared web hosting rarely allows you to alter the `php.ini` file, let alone reload the PHP/server process to take the INI changes into account. This is what @jeremy-castelli mean't I presume. – ojrask Oct 06 '15 at 19:57
  • There is a difference and I have observed that it can be pain sometimes as you simply can not use the parse_ini_file() standard PHP function on .env files. And if you want to use Dotenv to manipulate the loading of your .env files (with custom file name in a custom folder) at run-time, it's not very straightforward to do that...E.g take for a instance that you have to build the keys and value from some meta tags in your AWS instance and store the .env as some custom name in a specific folder under laravel app. – Andy Oct 19 '17 at 23:08
  • 1
    @PuneetSingh, in the context of microservices, dotenv project is an intent to rationalize the configuration init process. Java has .property files, Php ini files, Node json files... Dotenv through .env files is the mutual answer. No more coupling between the configuration format and the consumer language. Also contrary to the property files, .env file are not packaged with artifact. Making artifact truly identical between environments; – chaiyachaiya Feb 17 '20 at 10:59

3 Answers3

4

I think there are too many smartasses with nothing else to do. I will use parse_ini_file() and *.ini files. Everything is the same !

Toni Zeidler
  • 163
  • 1
  • 11
1

.env allows loading the configuration to the system environment. Straight loading of INI files does not do this unless you roll your own solution using putenv or similar for all the configuration values. Otherwise you could whichever approach you wish.

ojrask
  • 2,799
  • 1
  • 23
  • 23
0

According to the docs at https://github.com/vlucas/phpdotenv

Why .env?

You should never store sensitive credentials in your code. Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments – such as database credentials or credentials for 3rd party services – should be extracted from the code into environment variables.

Basically, a .env file is an easy way to load custom configuration variables that your application needs without having to modify .htaccess files or Apache/nginx virtual hosts. This means you won't have to edit any files outside the project, and all the environment variables are always set no matter how you run your project - Apache, Nginx, CLI, and even PHP 5.4's built-in webserver. It's WAY easier than all the other ways you know of to set environment variables, and you're going to love it.

NO editing virtual hosts in Apache or Nginx

NO adding php_value flags to .htaccess files

EASY portability and sharing of required ENV values

COMPATIBLE with PHP's built-in web server and CLI runner

Additionally, there is some extended functionality with Laravel's .env file. For example, you can reference other variables inside the .env file.

MAIL_USERNAME=admin@server.com
MAIL_FROM_ADDRESS=${MAIL_USERNAME}
Community
  • 1
  • 1
user1669496
  • 32,176
  • 9
  • 73
  • 65