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'),
];
1 Like

thank you very much, works great!

1 Like

The dotenv plugin for loading .env files has been updated to work with the upcoming release of Kirby v5. It also has a shiny new feature that automatically loads the correct file based on your environment, which is great for local development or staging setups.

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=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 // ...

The README also explains a little better how to use secrets within the Kirby config a little bit better than it did before.

/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'),
                ],
            ],
        ];
    }
];
1 Like