bnomei/kirby3-dotenv
Composer 安装命令:
composer require bnomei/kirby3-dotenv
包简介
Kirby Plugin for environment variables from .env
关键字:
README 文档
README
Kirby Plugin for environment variables from .env files
Installation
- unzip master.zip as folder
site/plugins/kirby3-dotenvor git submodule add https://github.com/bnomei/kirby3-dotenv.git site/plugins/kirby3-dotenvorcomposer require bnomei/kirby3-dotenv
Usage
Create a .env file in the root of your Kirby installation. Within your Kirby project you can access the environment variables using various PHP helper functions.
in production or with default environment
Based on an environment like
/site/config/config.phpKirby configuration file and given a/.envfile.
/.env
APP_MODE=production APP_DEBUG=false ALGOLIA_APIKEY=12d7331a21d8a28b3069c49830f463e833e30f6d KIRBY_API_USER=bnomei KIRBY_API_PW=52d3a0edcc78be6c5645fdb7568f94d3d83d1c2a
<?php echo $_ENV['APP_MODE']; // production echo env('APP_DEBUG'); // false // or echo $page->getenv('ALGOLIA_APIKEY'); // 12d7331... echo $page->env('ALGOLIA_APIKEY'); // 12d7331... echo site()->getenv('ALGOLIA_APIKEY'); // 12d7331... echo site()->env('ALGOLIA_APIKEY'); // 12d7331...
on local or staging test server
You can also create files to have different settings for different environments.
Set the environment explicitly with KIRBY_HOST or with the bnomei.dotenv.environment option.
The plugin does not use the request Host header as a trusted environment selector by default.
Based on an environment like
KIRBY_HOST=dotenv.testor abnomei.dotenv.environmentoption returningdotenv.test,/site/config/config.dotenv.test.phpKirby configuration file and given a/.env.dotenv.testfile.
/.env.dotenv.test
APP_MODE=staging APP_DEBUG=true ALGOLIA_APIKEY=950306d052ec893b467f2ca088daf2964b9f9530 KIRBY_API_USER=notBnomei KIRBY_API_PW=37e30ad867ff3a427317dcd1852abbd692b39ffc
<?php echo $_ENV['APP_MODE']; // staging echo env('APP_DEBUG'); // true // ...
/site/config/config.php
<?php return [ 'bnomei.dotenv.environment' => function () { return 'dotenv.test'; }, ];
If you intentionally want to select dotenv profiles from request hosts, use an explicit allowlist in your own callback:
<?php return [ 'bnomei.dotenv.environment' => function () { $host = explode(':', $_SERVER['HTTP_HOST'] ?? '')[0]; $allowed = ['dotenv.test', 'staging.example.com']; return in_array($host, $allowed, true) ? $host : ''; }, ];
Default values
In case you want to provide a default value as fallback in case the environment variable is not set you can do that with the 2nd parameter in each helper function. Thanks for your PR @teichsta.
<?php // `true` as default value echo env('ALGOLIA_ENABLED', true);
Usage in Config files might require a manual load
The environment variables set by your hosting service are available in your PHP scripts by default using the $_ENV super-global and the getenv() function. They are injected into the PHP environment by the web server.
But the values from the .env files are NOT available in the same way. They need to be loaded manually. The Dotenv plugin will load these on it's first usage. But the plugin can only do so automatically once it has been loaded itself which is after the config files have been parsed by Kirby.
Using Callbacks or the Ready Option
Where possible you should use the callback option to provide a function that returns the value you need. Once the callback is called the .env file will already have been loaded and the value will be available. Not all options support callbacks though.
/site/config/config.php
<?php return [ // does not support callbacks // 'debug' => false, // some plugins support callbacks for sensitive data options 'bnomei.seobility.apikey' => function() { return env('SEOBILITY_APIKEY'); }, // alternatively you can use the `ready` option // https://getkirby.com/docs/reference/system/options/ready 'ready' => function() { return [ 'debug' => env('APP_DEBUG', false), 'email' => [ 'transport' => [ 'type' => 'smtp', 'host' => 'smtp.postmarkapp.com', 'port' => 587, 'security' => true, 'auth' => true, 'username' => env('POSTMARK_USERNAME'), 'password' => env('POSTMARK_PASSWORD'), ], ], ]; } ];
Manually load the .env file in the config file
If you still decide you need to use the values from the .env files in the config files of Kirby directly, you can manually initialize the loading of these like so:
/site/config/config.php
<?php require_once __DIR__ . '/../plugins/kirby3-dotenv/global.php'; loadenv(); return [ 'debug' => env('APP_DEBUG', false), //... ];
You can provide a custom options as an array to that function if you want to force loading a specific directory or file.
Other Environment Variables Sources
If you set environment variables in your server configuration these will be available as well.
Warning
This plugin will load environment variables from .env files and potentially overwrite existing environment
variables.
Warning
Do not keep staging, test or other non-production dotenv files in production dotenv directories unless your
bnomei.dotenv.environment callback uses a trusted explicit value or an allowlist.
In the CLI
When running Kirby commands in the CLI make sure you prefix the command with the environment variable for the HOST you want to use.
env KIRBY_HOST=dotenv.test kirby my:command
Similar Plugins
Settings
| bnomei.dotenv. | Default | Description |
|---|---|---|
| dir | callback |
returning kirby()->roots()->index(). When installing Kirby 3 with Composer use a function() { return realpath(kirby()->roots()->index() . '/../'); } |
| file | .env |
|
| environment | callback |
trusted current environment selector; defaults to KIRBY_HOST only |
| required | callback or [] |
You can define required variables in the settings using an array. If any of these is missing a RuntimeException will be thrown. |
| setup | callback |
perform additional tasks on raw dotenv class instance |
Dependencies
Disclaimer
This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.
License
It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.
Credits
based on K2 version of
bnomei/kirby3-dotenv 适用场景与选型建议
bnomei/kirby3-dotenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 48.46k 次下载、GitHub Stars 达 41, 最近一次更新时间为 2018 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「env」 「dotenv」 「kirby」 「.env」 「enviroment」 「dotenv-loader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bnomei/kirby3-dotenv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bnomei/kirby3-dotenv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bnomei/kirby3-dotenv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An environment variable convenience library extension for vlucas/phpdotenv
Zero-dependency global `kirbylog()` helper for any content
Courier offers a convenient and painless solution for creating emails tailored for your Kirby website.
Laravel dotenv manager
Blade template for Kirby
Utils to load, parse and work with configuration on Mezzio projects
统计信息
- 总下载量: 48.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 41
- 点击次数: 13
- 依赖项目数: 1
- 推荐数: 4
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-18