machaven/laravel-cascading-config
Composer 安装命令:
composer require machaven/laravel-cascading-config
包简介
Multiple cascading environmental configuration files support for Laravel 5
README 文档
README
Why? - The .env problem
When continously developing and extending a laravel application, you may find that your .env file becomes large and hard to maintain. When multiple branches with .env changes are deployed, it becomes a nightmare to consolidate all the .env changes and update the live .env file.
This forced me to rethink configuration and what is truly important inside an .env file.
What to keep in your .env file
- Standard laravel .env settings
- Sensitive information, like login credentials for other apis.
What to take out of your .env file
- All custom settings that might seldom change.
Why use cascading config files?
-
Cascading configuration files allow you to keep a set of config files for every environment. All environment configs override the base configs in the config folder. This allows you to setup a dynamic config for each environment.
-
Settings changes can be recorded on pull requests. This prevents settings changes from being lost when only changed on the server .env file.
Furthermore if you use the env() function in your config files, it will allow you to override any defaults in the config file with variables from the .env file if they exist.
Install
Install the package through composer
composer require machaven/laravel-cascading-config
After installing, add the ServiceProvider to the providers array in app/config/app.php (not required for laravel 5.5+)
Machaven\LaravelCascadingConfig\CascadingConfigProvider::class,
How this works
The service provider will check if the directory exists for the environment configured in your .env file. If the environment folder exists, then all configuration files will be read from it and merged over the configurations from the config folder.
Example for cascading config files
Firstly, create your default config file in the standard laravel config/ folder.
config/example.php
Now create a folder to override the example config for an environment. A folder can be created for any environment defined in your APP_ENV. In this example, we use local:
config/local/
Now create another example config in the local folder:
config/local/example.php
Example config file contents
config/example.php
<?php
return [
'clientApi' => [
'curlTimeout' => env('CLIENT_API_CURL_TIMEOUT', 5),
'baseUrl' => env('CLIENT_API_BASE_URL', 'http://test.com/api/'),
'username' => env('CLIENT_API_USERNAME'), // Always read username and password from .env
'password' => env('CLIENT_API_PASSWORD'),
],
'test' => env('TEST', 'prod'),
];
config/local/example.php
<?php
return [
'clientApi' => [
'baseUrl' => env('CLIENT_API_BASE_URL', 'http://test.local/api/'),
],
'test' => env('TEST', 'local'),
];
In the local configuration above, we are not overriding the curlTimeout array key.
Example config results
When APP_ENV is local:
>>> config('example');
=> [
"clientApi" => [
"curlTimeout" => 5, // This is merged from the config/example.php config file
"baseUrl" => "http://test.local/api/",
"username" => "username in .env file",
"password" => "password in .env file",
],
"test" => "local",
]
When APP_ENV is prod:
>>> config('example');
=> [
"clientApi" => [
"curlTimeout" => 5,
"baseUrl" => "http://test.com/api/",
"username" => "username in .env file",
"password" => "password in .env file",
],
"test" => "prod",
]
As you can see, the above file is not overridden by the one in the config/local/ folder.
When APP_ENV is prod and TEST='ENV FILE' is added to .env file:
>>> config('example');
=> [
"clientApi" => [
"curlTimeout" => 5,
"baseUrl" => "http://test.com/api/",
"username" => "username in .env file",
"password" => "password in .env file",
],
"test" => "ENV FILE",
]
The test key is overridden here by the TEST variable in the .env file. This is because we are using the env() helper in our config files to override settings for any environment; if they exit in the .env.
machaven/laravel-cascading-config 适用场景与选型建议
machaven/laravel-cascading-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.39k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「config」 「laravel」 「environment」 「cascading」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 machaven/laravel-cascading-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 machaven/laravel-cascading-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 machaven/laravel-cascading-config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
The tenancy/tenancy identification driver using environment variables
A Zend Framework module to quickly and easily set PHP settings.
An environment variable convenience library extension for vlucas/phpdotenv
Utils to load, parse and work with configuration on Mezzio projects
Server environment detection based on config array-file
统计信息
- 总下载量: 9.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-24