ENV Component vlucas/phpdotenv

Description

vlucas/phpdotenv is a component for loading environment variables, used to distinguish configurations for different environments (such as development, testing, etc.).

Project Address

https://github.com/vlucas/phpdotenv

Installation

composer require vlucas/phpdotenv

Usage

Create a .env file in the project root directory

.env

DB_HOST = 127.0.0.1
DB_PORT = 3306
DB_NAME = test
DB_USER = foo
DB_PASSWORD = 123456

Modify the configuration file

config/database.php

return [
    // Default database
    'default' => 'mysql',

    // Various database configurations
    'connections' => [
        'mysql' => [
            'driver'      => 'mysql',
            'host'        => getenv('DB_HOST'),
            'port'        => getenv('DB_PORT'),
            'database'    => getenv('DB_NAME'),
            'username'    => getenv('DB_USER'),
            'password'    => getenv('DB_PASSWORD'),
            'unix_socket' => '',
            'charset'     => 'utf8',
            'collation'   => 'utf8_unicode_ci',
            'prefix'      => '',
            'strict'      => true,
            'engine'      => null,
        ],
    ],
];

Tip
It is recommended to add the .env file to the .gitignore list to avoid committing it to the code repository. Add a .env.example configuration sample file to the repository, and when deploying the project, copy .env.example to .env and modify the configurations in .env according to the current environment. This way, the project can load different configurations in different environments.

Note
vlucas/phpdotenv may have bugs in the PHP TS version (thread-safe version); please use the NTS version (non-thread-safe version).
The current version of PHP can be checked by executing php -v.

More Information

Visit https://github.com/vlucas/phpdotenv