The ConfigUtil in the LaraUtilX package provides functionality for managing dynamic configuration settings.
Settings are persisted as JSON on a filesystem disk, so they survive between requests and deploys independently of your config/ files. The location is configurable:
'config' => [
'disk' => null, // null uses the default filesystem disk
'path' => 'config/settings.json', // relative to that disk's root
],
The path is relative to the disk root. An absolute path would be appended to the root and written somewhere it could never be read back from.
getAllSettings(?string $path = null): array: Retrieves all dynamic configuration settings, optionally from an explicit path.getSetting(string $key, mixed $default = null): mixed: Retrieves a specific setting. Supports dot notation.setSetting(string $key, mixed $value): void: Sets or updates a setting. Supports dot notation.forgetSetting(string $key): void: Removes a setting. Supports dot notation.getAllAppSettings(): array: Retrieves all application settings from config('app').Changed in v1.5.5.
forgetSetting()now honours dot notation; previously it usedunset(), which could not reach a nested key, soforgetSetting('mail.from')silently did nothing.Changed in v1.5.4.
getAllSettings()no longer takes a second$keyargument,getSetting()accepts a default, andforgetSetting()is new. Earlier releases were non-functional:getSetting()always returned null, andsetSetting()wrote to a path that could never be read back, so there is nothing to migrate.
Get All Settings:
To retrieve all dynamic configuration settings, use the getAllSettings method:
use LaraUtilX\Utilities\ConfigUtil;
class MyController extends Controller
{
protected $configUtil;
public function __construct(ConfigUtil $configUtil)
{
$this->configUtil = $configUtil;
}
public function index()
{
$settings = $this->configUtil->getAllSettings();
// Use the retrieved settings
}
}
Get Specific Setting:
To retrieve a specific dynamic configuration setting, use the getSetting method:
use LaraUtilX\Utilities\ConfigUtil;
class MyController extends Controller
{
protected $configUtil;
public function __construct(ConfigUtil $configUtil)
{
$this->configUtil = $configUtil;
}
public function getSpecificSetting()
{
$settingValue = $this->configUtil->getSetting('key_name');
// With a fallback when the key has never been set
$theme = $this->configUtil->getSetting('theme', 'light');
// Dot notation reaches into nested values
$sender = $this->configUtil->getSetting('mail.from');
}
}
Set or Update Setting:
To set or update a dynamic configuration setting, use the setSetting method:
use LaraUtilX\Utilities\ConfigUtil;
class MyController extends Controller
{
protected $configUtil;
public function __construct(ConfigUtil $configUtil)
{
$this->configUtil = $configUtil;
}
public function setOrUpdateSetting()
{
$settingKey = 'key_name';
$settingValue = 'setting_value';
$this->configUtil->setSetting($settingKey, $settingValue);
}
}
Get All Application Settings:
To retrieve all application settings, use the getAllAppSettings method:
use LaraUtilX\Utilities\ConfigUtil;
class MyController extends Controller
{
protected $configUtil;
public function __construct(ConfigUtil $configUtil)
{
$this->configUtil = $configUtil;
}
public function getAllAppSettings()
{
$appSettings = $this->configUtil->getAllAppSettings();
// Use the retrieved application settings
}
}
The utility provides convenient methods for managing dynamic configuration settings in Laravel applications.
Success Result: Dynamic configuration settings can be efficiently retrieved, updated, or set using the provided methods.
Error Result: If an error occurs during retrieval, updating, or setting of configuration settings, appropriate error handling mechanisms should be implemented based on application requirements.
You can publish this utility through the below command:
php artisan vendor:publish --tag=lara-util-x-config
This utility simplifies the process of managing configuration settings in Laravel applications, providing flexibility and customization options.