beebmx / kirby-env
Enable env variables to Kirby
Requires
- php: ^8.4
- getkirby/composer-installer: ^1.2
- vlucas/phpdotenv: ^5.6.1
Requires (Dev)
- getkirby/cms: ^4.9 || ^5.4 || ^6.0
- laravel/pint: ^1.0
- pestphp/pest: ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Env use the vlucas/phpdotenv package and enable their features for Kirby.
This package should be used if you want to store your project credentials or variables in a separate place from your code or if you want to have development and production access in different places.
Compatibility
| Plugin | Kirby 3 | Kirby 4 | Kirby 5 | Kirby 6 | PHP |
|---|---|---|---|---|---|
| 6.x | ❌ | ✅ | ✅ | ✅ | 8.4+ |
| 5.2.x | ❌ | ✅ | ✅ | ❌ | 8.3+ |
| 5.0 – 5.1 | ✅ | ✅ | ✅ | ❌ | 8.2+ |
| 4.2.x | ✅ | ✅ | ❌ | ❌ | 8.1+ |
| 4.0 – 4.1 | ✅ | ❌ | ❌ | ❌ | 8.0+ |
| 1.x – 3.x | ✅ | ❌ | ❌ | ❌ | 7.x+ |
Note
The PHP versions you can use also depend on your Kirby version. With the plugin 6.x: Kirby 4 runs on PHP 8.4, Kirby 5 on PHP 8.4 – 8.5 and Kirby 6 on PHP 8.4 – 8.6.
Kirby 3 only runs up to PHP 8.3, so it is not supported by the plugin 6.x. Use the plugin 5.1.x instead.
Kirby 3 has reached its end of life. Kirby 3 and several of its pinned dependencies have open security advisories,
and Composer blocks those packages by default, so installing on a Kirby 3 site fails until you allow it in your
project's composer.json with "config": { "policy": { "advisories": { "block": false } } }.
Installation
Installation with composer
Install the plugin version that matches your Kirby version:
| Kirby | Plugin | Command |
|---|---|---|
| 6.x | 6.x | composer require beebmx/kirby-env |
| 5.x | 6.x | composer require beebmx/kirby-env |
| 4.x | 6.x | composer require beebmx/kirby-env |
| 3.x | 5.1.x | composer require beebmx/kirby-env:"~5.1.0" |
Composer installs the newest plugin version your PHP version allows, e.g. the plugin 5.2.x on PHP 8.3 with Kirby 4 or Kirby 5.
Usage
You don't need to do anything if your want to access in any $page, just use the page method:
$page->env('VAR');
But if you want to set variables in your config.php file, first you need to load the object with:
\Beebmx\KirbyEnv::load('main/path');
You need to have an .env file in your main/path directory.
You can store any credentials or variables secure like:
KIRBY_DEBUG=false
SECRET_KEY=my_secret_key
PUBLIC_KEY=my_public_key
FOO=BAR
BAZ=${FOO}
Options
When you create an instance of \Beebmx\KirbyEnv you need to load the environment with:
\Beebmx\KirbyEnv::load();
If you require the immutability provided by vlucas/phpdotenv, just:
\Beebmx\KirbyEnv::overload();
Example
Here's an example of a configuration in config.phpfile:
<?php \Beebmx\KirbyEnv::load('main/path'); return [ 'debug' => env('KIRBY_DEBUG', false), 'SECRET' => env('SECRET_KEY'), 'PUBLIC' => env('PUBLIC_KEY'), ];
Usage note
It is important that you add to your .gitignore the .env file.
The main/path is where the .env file is located.