A simple wrapper around vlucas' PHP dotenv library for Kirby CMS.

Overview

kirby-phpdotenv

A simple wrapper around vlucas' PHP dotenv library for Kirby CMS.

Why?

I've been using .env in my Kirby projects for a while, but I got tired of pasting in the env() function for every new site. So, I created this package instead.

Quick Example

# .env
APP_DEBUG=true
# site/config/config.php
c::set('debug', env('APP_DEBUG', false));
# elsewhere
c::get('debug'); // true

Install

Install via composer

composer require jevets/kirby-phpdotenv 

Usage

1. Instantiate

In your site's index.php file (or in site.php file), load Composer's autoloader.

require_once 'path/to/vendor/autoload.php';

Then instantiate with:

  1. The path where dotenv can find your .env file
  2. A file name other than .env
new Jevets\Kirby\Dotenv\Dotenv(__DIR__ . DS, '.env');

Note: Jevets\Kirby\Dotenv\Dotenv will instantiate and run [dotenv]->load() for you.

2. Create your .env file

KIRBY_LICENSE=1234-12345-123456

APP_MODE=production
APP_DEBUG=true

3. Use in config.php file(s)

c::set('license', env('KIRBY_LICENSE', 'put your license in .env'));
c::set('debug', env('APP_DEBUG', false));

c::get('license'); // "1234-12345-123456"
c::get('debug'); // true

This library loads a global helper function:

function env($key, $default = '') {...}

Note function env() declaration is wrapped in if (!function_exists('env')) so you can override it or use your own method.

Customized folder setup

In my Kirby projects, I usually keep this folder structure:

project/
  .env
  .env.example
  .gitignore
  package.json
  composer.json
  site/
    accounts/
    blueprints/
    cache/
    config/
    snippets/
    templates/
  public/
    kirby/
      toolkit/
    panel/
    content/
    assets/
    thumbs/
    index.php
    site.php
  src/
  vendor/

And here's my typical site.php

# Composer autoload
require __DIR__ . DS . '..' . DS . 'vendor' . DS . 'autoload.php';

# DotEnv
new Jevets\Kirby\Dotenv\Dotenv(__DIR__ . DS . '..');

# Setup Kirby
$kirby = kirby();
$kirby->roots()->site = __DIR__ . DS . '..' . DS . 'site';

ChangeLog

0.0.3

  • Rename class alias in helpers.php from String to Str for PHP7 compatibility

0.0.2

  • Initial release

Collaborating

Pull requests and suggestions are quite welcome!

Credits

  • All credit for dotenv goes to vlucas for his great work on PHP dotenv. I just wrapped it up in a package to speed up development of Kirby-based sites.
  • I stole Taylor Otwell's env() function from Laravel 5.1, with a couple tweaks to get it working with Kirby.

Bugs/Issues

Please use the issue tracker on the GitHub repo.

You might also like...
Kirby 3 Plugin for running jobs like cleaning the cache from within the Panel, PHP code, CLI or a cronjob

Kirby 3 Janitor Kirby 3 Plugin for running jobs. It is a Panel Button! It has jobs build-in for cleaning the cache, sessions, create zip-backup, pre-g

Kirby Janitor job for staging

Simple staging Janitor jobs Plugin for very simple staging setup for https://github.com/bnomei/kirby3-janitor/ (required). Beta quality - use at your

This plugin allows you to create many-to-many relationships between pages in Kirby and synchronizes them on both sides.
This plugin allows you to create many-to-many relationships between pages in Kirby and synchronizes them on both sides.

Kirby 3 Many To Many Field This plugin allows you to create many-to-many relationships between pages in Kirby.

Color-managed thumbnails for Kirby 3

ImageKit for Kirby 3 This is not directly related for ImageKit for Kirby 2, but based on the same idea of improving Kirby’s built-in image processing

Helper to automatically load various Kirby extensions in a plugin

Autoloader for Kirby Helper to automatically load various Kirby extensions in a plugin Commerical Usage This package is free but if you use it in a co

Add information about PGP public keys on upload in Kirby v3
Add information about PGP public keys on upload in Kirby v3

Kirby3 GnuPG This plugin adds information about PGP public keys on upload, using gpg binary (which needs to be installed for this to work). Getting st

A Frankenstein's monster: Kirby inside of Laravel.

A Kirby-Laravel Starter Kit This is an experimental starter kit for using Kirby within Laravel (a little like Statamic). In my limited experience it r

Plugin for Kirby that allows you to load assets generated by Vite.

Kirby Vite Plugin Plugin for Kirby that allows you to load assets generated by Vite. In development mode, assets are loaded from Vite's development se

🦭 Kirby, but headless only – KQL with bearer token, Express-esque middlewares & more

Kirby Headless Starter ℹ️ Send a Bearer test authorization header with a request to the live playground to test this headless starter. This starter ki

Comments
  • add overload param to constructure method

    add overload param to constructure method

    PR contains the following

    • Added param constructer with default param (false)
    • Added comment to constructer method
    • Added condition to initialize the dotENV library

    I think this will be a nice feature because sometimes I need to overwrite environment variables which already set up by hosting provider.

    opened by barnabas-szekeres 0
Owner
Steve Jamesson
Full stack web developer
Steve Jamesson
Perch Dashboard app for exporting content to (Kirby) text files and Kirby Blueprint files

toKirby Perch Dashboard app for exporting content to (Kirby) text files and Kirby Blueprint files. You can easily install and test it in a few steps.

R. Banus 4 Jan 15, 2022
This Kirby V3 Plugin brings snippets and blueprints together in one place. It includes useful tools that completely changing the way you work with Kirby: Fast and well organized.

Kirby Components Overview Do you love to make awesome projects with Kirby CMS? Do you also find it difficult to switch between snippets and blueprints

Roman Gsponer 6 May 31, 2023
This is a simple Wrapper around the ZipArchive methods with some handy functions

Note I haven't updated this package in a long time except merging PRs. The last time I was using this package was with PHP5. I archived the repository

Nils Plaschke 845 Dec 13, 2022
This is a simple Wrapper around the ZipArchive methods with some handy functions

Note I haven't updated this package in a long time except merging PRs. The last time I was using this package was with PHP5. I archived the repository

Nils Plaschke 836 Jan 26, 2022
Kirby wrapper for automated content accessibility checkers Editoria11y and Sa11y

Kirby3 A11yprompter For a comprehensive overview of Sa11y and Editoria11y, how they can assist maintaining an accessible website by supporting content

Sebastian Greger 8 Apr 25, 2022
An object oriented wrapper around PHP's built-in server.

Statix Server Requirements PHP 8 minumum Installation composer require statix/server Basic Usage To get started, ensure the vendor autoload script is

Statix PHP 113 Dec 27, 2022
A PHP wrapper around Libreoffice for converting documents from one format to another.

Document Converter A PHP wrapper around Libreoffice for converting documents from one format to another. For example: Microsoft Word to PDF OpenOffice

Lukas White 0 Jul 28, 2022
WordPlate is a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

WordPlate is simply a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

WordPlate 1.7k Dec 24, 2022
A wrapper around faker for factory muffin

Factory Muffin Faker 2.3 The goal of this package is to wrap Faker to make it super easy to use with Factory Muffin. Note that this library does not a

The League of Extraordinary Packages 36 Nov 29, 2022
A wrapper around symplify/config-transformer used to update recipes and using easy coding standard for generating readable config files.

Symfony Recipes Yaml to PHP Converter This is a wrapper around the symplify/config-transformer used to convert Symfony core recipes which uses .yaml c

Alexander Schranz 3 Nov 24, 2022