Download the PHP package justenough/craft-siteawareconfig without Composer
On this page you can find all versions of the php package justenough/craft-siteawareconfig. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download justenough/craft-siteawareconfig
More information about justenough/craft-siteawareconfig
Files in justenough/craft-siteawareconfig
Package craft-siteawareconfig
Short Description Set & retrieve site-aware config values
License MIT
Informations about the package craft-siteawareconfig
Multi Site Config plugin for Craft CMS 3.x
What?
Set & retrieve Craft CMS config file values that are site-aware, with a clean API and easy defaults.
Why?
When working with multi-site Craft CMS installs, you may find yourself needing config values than should exist for all sites in an install, but vary in value between the sites themselves.
This plugin allows you to share your template code / partials across sites, while always getting the correct value for the current site (or for a different site, if required).
Example usecases:
- API keys
- GA / GTM IDs
- Social network links
- Asset paths (logos etc)
Why use this instead of
- Logic in my templates? That works, but it's ugly as sin, and quickly gets repetitive. As much as possible, templates should just format and display data that is passed from PHP
- A Craft global? You could, but this is much faster (no DB / fields overhead), more lightweight and allows for both defaults and sharing values between sites / groups of sites if required
- Environment variables? You should use Environment Variables for config whenever you can! But with multi-site installs they're often not enough on their own. You can (and should) use
App::env()
inside yourconfig/siteware.php
to set values that belong in the environment (like API keys) that you may need to maintain per-site. - Custom key in
config/general.php
? For basic use cases, this works fine, but again setting defaults / fallback values gets complex quickly.
How?
This plugin uses Craft's built-in ConfigHelper::localizedValue()
helper to parse values in a config/siteaware.php
config file and make them site aware. You can then consume them in your template / PHP code without having to worry about getting the right value for the right site.
See configuration below for config file syntax.
Requirements
This plugin requires PHP 7.4+ and Craft CMS 3.5 or later. It will probably work with older versions of Craft, but is untested with them.
Installation
Configuration
Edit the config file at config/siteaware.php
to set your config values.
Retrieving config values
Templating
Config values for the current Site are exposed to Twig under craft.siteAwareConfig.currentSite
:
If needed, you can access the config for any site by passing its handle to craft.siteAwareConfig.forSite()
PHP
An instance of justenough\siteawareconfig\services\Config
is what gets exposed to Twig. Should you need to, you can also access site aware config settings from PHP-land:
The returned SiteAwareConfig
extends Yii's BaseObject
.
Caveats / Potential "Gotchas"
While values in multi-environment configs are merged, site-aware values using the array syntax are replaced. This is usually only an issue if you are using an associative array for your config value (for example to group related values together).
If you need to merge values between sites either flatten your values, or use a callable to handle the merging/default logic.
Attributions
Brought to you by Just Enough Consulting