lucasmichot/laraveldotenv

轻松设置Laravel项目的环境变量,通过针对每个环境的设置加速部署

1.0.7 2014-01-22 15:22 UTC

This package is not auto-updated.

Last update: 2023-09-30 10:29:27 UTC


README

一个用于为您的Laravel项目设置环境变量文件的简单实用工具。

Latest Stable Version Total Downloads

要求/加载

在您的 composer.json 中要求此包并运行 composer update。或者直接运行 composer require lucasmichot/laravel-ide-laraveldotenv:dev-master

"require": {
    "lucasmichot/laraveldotenv": "dev-master"
}

运行 composer update 后,在 app/config/app.php 中 Providers 数组的底部添加 LaraveldotenvServiceProvider

'providers' => array(
    //  ...
    'Lucasmichot\Laraveldotenv\LaraveldotenvServiceProvider',
),
````

## Usage

Place a file called `.env` at the root of your Laravel project folder, so that `.env` and `server.php` files are in the same folder.

You can put all the required environment variable with this `.env` file, respecting the following syntax: `ENV_KEY=ENV_VALUE`. In this example we set environment variables for MySQL database configuration and access:

Content of `.env` file:

````ìni
DB_HOST=localhost
DB_NAME=database-name
DB_USER=root
DB_PASS=password
````

Keys and values are automatically recognized and processed so than you could even write your `.env` file this way and get the same result as above:

````ìni
DB_HOST   =    localhost
DB_NAME=  "database-name"
DB_USER="root"

DB_PASS=    password
````


Then in your `database.php` config file, you can use the existing variables in `$_ENV` array:

````php
'mysql' => array(
    'driver'    => 'mysql',
    'host'      => $_ENV['DB_HOST'],
    'database'  => $_ENV['DB_NAME'],
    'username'  => $_ENV['DB_USER'],
    'password'  => $_ENV['DB_PASS'],
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),
````

#### Note

Keep in mind that this `.env` file must shall not be committed in your repository and must be excluded from release archive. 
Ensure to add `.env` to your `.gitignore` file.

To ensure this file will not be available in the the release archive, simply add `.env export-ignore` to your `.gitattributes` file.

## Todo

- Add tests.

## License

Released under the MIT License - see `LICENSE.txt` for details.