Kirby3 DotEnv - load enviroment variables from.env files

I am creating posts for my plugins to make it easier to find them using the forum search and not just the docs search.

Kirby 3 Plugin for environment variables from .env with support for multi enviriment setups like for staging servers. It can also be used inside config files. It provides a modern and secure global helper env() to load your variables.

/.env

APP_MODE=production
APP_DEBUG=false
ALGOLIA_APIKEY=12d7331a21d8a28b3069c49830f463e833e30f6d
KIRBY_API_USER=bnomei
KIRBY_API_PW=52d3a0edcc78be6c5645fdb7568f94d3d83d1c2a

/.env.staging

APP_MODE=staging
APP_DEBUG=true
ALGOLIA_APIKEY=950306d052ec893b467f2ca088daf2964b9f9530
KIRBY_API_USER=notBnomei
KIRBY_API_PW=37e30ad867ff3a427317dcd1852abbd692b39ffc

on server

echo $_ENV['APP_MODE']; // production
echo env('APP_DEBUG'); // false
// or
echo $page->getenv('ALGOLIA_APIKEY'); // 12d7331a21d8a28b3069c49830f463e833e30f6d
echo $page->env('ALGOLIA_APIKEY'); // 12d7331a21d8a28b3069c49830f463e833e30f6d
echo site()->getenv('ALGOLIA_APIKEY'); // 12d7331a21d8a28b3069c49830f463e833e30f6d
echo site()->env('ALGOLIA_APIKEY'); // 12d7331a21d8a28b3069c49830f463e833e30f6d

on staging server

echo $_ENV['APP_MODE']; // staging
echo env('APP_DEBUG'); // true
// or
echo $page->getenv('ALGOLIA_APIKEY'); // 37e30ad867ff3a427317dcd1852abbd692b39ffc
echo $page->env('ALGOLIA_APIKEY'); // 37e30ad867ff3a427317dcd1852abbd692b39ffc
echo site()->getenv('ALGOLIA_APIKEY'); // 37e30ad867ff3a427317dcd1852abbd692b39ffc
echo site()->env('ALGOLIA_APIKEY'); // 37e30ad867ff3a427317dcd1852abbd692b39ffc

site/config/config.php

<?php

return [
    // other options
    'myapikey' => env('ALGOLIA_APIKEY'),
];

thank you very much, works great!

1 Like