Configuration Utility

The ConfigUtil in the LaraUtilX package provides functionality for managing dynamic configuration settings.

Where settings are stored

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.

Methods

  1. getAllSettings(?string $path = null): array: Retrieves all dynamic configuration settings, optionally from an explicit path.
  2. getSetting(string $key, mixed $default = null): mixed: Retrieves a specific setting. Supports dot notation.
  3. setSetting(string $key, mixed $value): void: Sets or updates a setting. Supports dot notation.
  4. forgetSetting(string $key): void: Removes a setting.
  5. getAllAppSettings(): array: Retrieves all application settings from config('app').

Changed in v1.5.4. getAllSettings() no longer takes a second $key argument, getSetting() accepts a default, and forgetSetting() is new. Earlier releases were non-functional: getSetting() always returned null, and setSetting() wrote to a path that could never be read back, so there is nothing to migrate.

Usage

  1. 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
       }
    }
  2. 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');
        }
    }
  3. 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);
        }
    }
  4. 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
        }
    }

Result

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.

Publish

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.