Laravel 5 environment config arrays?

后端 未结 2 1527
借酒劲吻你
借酒劲吻你 2021-01-04 07:05

In Laravel 4, you could set an environment based config folder structure:

/config/app.php
/config/dev/app.php
/config/staging/app.php
/config/testing/app.php         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 07:28

    If you need to create an array on values, you can create on string format and when you need you can parse them

    MY_ARRAY_VALUE=1,2,house,cat,34234
    

    When you need them

    $myArrayValue  = explode(',', env('MY_ARRAY_VALUE'));
    

    Or save your values in JSON and get them with json_decode()

    $myArrayValue  = json_decode(env('MY_ARRAY_VALUE'), true);
    

    Extra info:

    On Laravel 5, you need to translate all your configs files in one .env file.

    On each environment your .env file will be diferent with values for this environment.

    To set your environment, you need to change the value of APP_ENV in your .env file

    APP_ENV=local
    

    And you can create your own variables in that file

    https://laravel.com/docs/5.2/configuration#environment-configuration

    This is an extract of the upgrade guide to Laravel 5.0 https://laravel.com/docs/5.2/releases#laravel-5.0

    Instead of a variety of confusing, nested environment configuration directories, Laravel 5 now utilizes DotEnv by Vance Lucas. This library provides a super simple way to manage your environment configuration, and makes environment detection in Laravel 5 a breeze. For more details, check out the full configuration documentation.

    You can find a default .env file here: https://github.com/laravel/laravel/blob/master/.env.example

    It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different cache driver locally than you do on your production server. It's easy using environment based configuration.

    To make this a cinch, Laravel utilizes the DotEnv PHP library by Vance Lucas. In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.

提交回复
热议问题