Introduction
Cartalyst's Composite Config package enhances illuminate/config
to allow configuration items to be placed within a database whilst cascading back to the filesystem.
This is super useful for building user interfaces that facilitate editing configuration for an app. Because it does not change the API for retrieving configuration items, it degrades gracefully to the filesystem if not present and requires zero changes to the places which use the configuration items.
The package requires PHP 7.3+ and follows the FIG standard PSR-4 to ensure a high level of interoperability between shared PHP code and is fully unit-tested.
Getting started
Have a read through the Installation Guide.
Quick Example
// Set config at runtime
Config::set($key, $value);
// Set persisting config at runtime
Config::persist($key, $value);
Installation
The best way to install the Composite Config package is quickly and easily done with Composer.
Open your composer.json
and add the following to the require
array
"cartalyst/composite-config": "^5.0"
Add the following lines after the require
array on your composer.json
file
"repositories": [
{
"type": "composer",
"url": "https://packages.cartalyst.com"
}
]
Note: Make sure your
composer.json
file is in a valid JSON format after the required changes.
Install the dependencies
Run Composer to install or update the new requirement.
php composer install
or
php composer update
Now you are able to require the vendor/autoload.php
file to PSR-0 autoload the library.
Example
// Include the composer autoload file
require_once 'vendor/autoload.php';
// Import the necessary classes
use Cartalyst\CompositeConfig\CompositeLoader;
use Illuminate\Config\Repository;
use Illuminate\Database\Connection;
use Illuminate\Filesystem\Filesystem;
// Setup config loader
$loader = new CompositeLoader(new Filesystem(), '/path/to/config/files');
// Attach the optional database loading functionality.
// Without this, the composite loader acts like the default file loader.
$database = new Connection(new PDO('mysql:dbname=my_database;host=127.0.0.1'), $prefix = '');
$loader->setDatabase($database);
// Instantiate config
$config = new Repository($loader);
// Set persisting config
$config->getLoader()->set($key, $value);
Integration
Laravel
The Composite Config package has optional support for Laravel 8 and it comes bundled with a Service Provider for easier integration.
After you have installed the package correctly, just follow the instructions.
Open your Laravel config file config/app.php
and add the following lines.
In the $providers
array add the following service provider for this package.
'Cartalyst\CompositeConfig\Laravel\CompositeConfigServiceProvider',
Assets
Run the following commands to publish the migration and config files.
php artisan vendor:publish --provider="Cartalyst\CompositeConfig\Laravel\CompositeConfigServiceProvider"
Migrations
Run the following command to migrate Sentinel after publishing the assets.
php artisan migrate
Configuration
After publishing, the composite config config file can be found under config/cartalyst.composite-config.php
where you can modify the package configuration.
Usage
Retrieving Config
Usage is identical to that explained in the Laravel documentation
Config::get($key);
Saving Config
There are two ways of saving configuration items.
1. Runtime
To set configuration at runtime, use
Config::set($key, $value);
During that request, calling Config::get($key);
will return the value you have set.
Note: Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests.
2. Persisting
To set persisting configuration at runtime, use
Config::persist($key, $value);