Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Kirby Plugin for environment variables from .env files

License

NotificationsYou must be signed in to change notification settings

bnomei/kirby3-dotenv

Repository files navigation

Kirby 5PHP 8.2ReleaseDownloadsCoverageMaintainabilityDiscordBuymecoffee

Kirby Plugin for environment variables from .env files

Installation

  • unzipmaster.zip as foldersite/plugins/kirby3-dotenvor
  • git submodule add https://github.com/bnomei/kirby3-dotenv.git site/plugins/kirby3-dotenv or
  • composer 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.php Kirby configuration file and given a
  • /.env file.

/.env

APP_MODE=productionAPP_DEBUG=falseALGOLIA_APIKEY=12d7331a21d8a28b3069c49830f463e833e30f6dKIRBY_API_USER=bnomeiKIRBY_API_PW=52d3a0edcc78be6c5645fdb7568f94d3d83d1c2a
<?phpecho$_ENV['APP_MODE'];// productionechoenv('APP_DEBUG');// false// orecho$page->getenv('ALGOLIA_APIKEY');// 12d7331...echo$page->env('ALGOLIA_APIKEY');// 12d7331...echosite()->getenv('ALGOLIA_APIKEY');// 12d7331...echosite()->env('ALGOLIA_APIKEY');// 12d7331...

on local or staging test server

You can also create files to have different settings for different environments.The plugin will try to automatically load the correct file based on the environment.

Based on an environment like

  • http://dotenv.test matching a
  • /site/config/config.dotenv.test.php Kirby configuration file and given a
  • /.env.dotenv.test file.

/.env.dotenv.test

APP_MODE=stagingAPP_DEBUG=trueALGOLIA_APIKEY=950306d052ec893b467f2ca088daf2964b9f9530KIRBY_API_USER=notBnomeiKIRBY_API_PW=37e30ad867ff3a427317dcd1852abbd692b39ffc
<?phpecho$_ENV['APP_MODE'];// stagingechoenv('APP_DEBUG');// true// ...

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 withthe 2nd parameter in each helper function. Thanks for your PR @teichsta.

<?php// `true` as default valueechoenv('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 thegetenv() 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 thecallback 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

<?phpreturn [// does not support callbacks// 'debug' => false,// some plugins support callbacks for sensitive data options'bnomei.seobility.apikey' =>function() {returnenv('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

<?phprequire_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 environmentvariables.

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.DefaultDescription
dircallbackreturningkirby()->roots()->index(). When installing Kirby 3 with Composer use afunction() { return realpath(kirby()->roots()->index() . '/../'); }
file.env
environmentcallbackauto-detection for the current environment
requiredcallback or []You can define required variables in the settings using an array. If any of these is missing aRuntimeException will be thrown.
setupcallbackperform 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 itin a production environment. If you find any issues,pleasecreate a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence orany other form of hate speech.

Credits

based on K2 version of


[8]ページ先頭

©2009-2025 Movatter.jp