### Install CodeIgniter Settings Source: https://github.com/codeigniter4/settings/blob/develop/README.md Use Composer to install the settings library. After installation, migrate your database to set up the necessary tables. ```bash composer require codeigniter4/settings ``` ```bash php spark migrate --all ``` -------------------------------- ### Install CodeIgniter Settings via Composer Source: https://github.com/codeigniter4/settings/blob/develop/docs/installation.md Use this command to install the library using Composer. This is the recommended installation method. ```bash composer require codeigniter4/settings ``` -------------------------------- ### Using the Settings Helper Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Load the 'setting' helper and use it to get, set, and forget configuration values. The helper provides a shortcut to the settings service. ```php helper('setting'); $name = setting('App.siteName'); // Store a value setting('App.siteName', 'My Great Site'); // Using the service through the helper $name = setting()->get('App.siteName'); $settings = setting()->getMany([ 'App.siteName', 'App.siteEmail', ]); setting()->set('App.siteName', 'My Great Site'); setting()->setMany([ 'App.siteName' => 'My Great Site', 'App.siteEmail' => 'support@example.com', ]); // Forgetting a value setting()->forget('App.siteName'); setting()->forgetMany([ 'App.siteName', 'App.siteEmail', ]); ``` -------------------------------- ### Explicit Batch SetMany Example Source: https://github.com/codeigniter4/settings/blob/develop/docs/configuration.md Demonstrates using the setMany() method for explicitly grouping multiple settings into a single API call. ```php $settings->setMany([ 'Example.prop1' => 'value1', 'Example.prop2' => 'value2', 'Example.prop3' => 'value3', ]); ``` -------------------------------- ### Deferred Writes Example Source: https://github.com/codeigniter4/settings/blob/develop/docs/configuration.md Illustrates how deferred writes batch multiple set() calls into a single database transaction at the end of the request. ```php // With deferWrites = false: 1 SELECT (hydrates all settings for context) + 3 separate INSERT/UPDATE queries $settings->set('Example.prop1', 'value1'); $settings->set('Example.prop2', 'value2'); $settings->set('Example.prop3', 'value3'); // With deferWrites = true: 1 SELECT + 1 INSERT batch + 1 UPDATE batch (all batched at end of request) ``` -------------------------------- ### FileHandler Deferred Writes Example Source: https://github.com/codeigniter4/settings/blob/develop/docs/configuration.md Illustrates how enabling deferWrites reduces I/O operations by batching multiple set/forget calls into a single file write at the end of the request. ```php // With deferWrites = false: 1 file read (hydrates all settings for class) + 3 separate file writes $settings->set('Example.prop1', 'value1'); $settings->set('Example.prop2', 'value2'); $settings->set('Example.prop3', 'value3'); // With deferWrites = true: 1 file read + 1 file write (all changes batched at end of request) ``` -------------------------------- ### Get a Configuration Value Source: https://github.com/codeigniter4/settings/blob/develop/docs/index.md Use this to retrieve a configuration value from the database. If the value is not found in the database, the default from the config file will be used. ```php service('settings')->get('App.siteName'); ``` -------------------------------- ### Retrieve a Single Setting Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use the `get()` method to retrieve a single configuration value using dot notation. This is equivalent to accessing `config('ClassName')->propertyName`. ```php service('settings')->get('App.siteName'); ``` -------------------------------- ### Configure FileHandler Source: https://github.com/codeigniter4/settings/blob/develop/docs/configuration.md Set up the FileHandler with options like class, path, writeable, and deferWrites. The handler automatically creates directories and checks permissions. ```php public $file = [ 'class' => FileHandler::class, 'path' => WRITEPATH . 'settings', 'writeable' => true, 'deferWrites' => false, ]; ``` -------------------------------- ### Publish Configuration File Source: https://github.com/codeigniter4/settings/blob/develop/docs/configuration.md Use this command to create a copy of the configuration file in app/Config/Settings.php for customization. ```bash php spark settings:publish ``` -------------------------------- ### Run All Database Migrations Source: https://github.com/codeigniter4/settings/blob/develop/docs/installation.md Execute this command to migrate all available database tables, including those for the CodeIgniter Settings package. ```bash php spark migrate --all ``` -------------------------------- ### Retrieve Contextual Setting using Helper Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md The `setting()` helper function can also be used to retrieve contextual settings, providing a more concise syntax. ```php $context = 'user:' . user_id(); setting()->get('App.theme', $context); ``` -------------------------------- ### Multiple Handlers Configuration Source: https://github.com/codeigniter4/settings/blob/develop/docs/configuration.md Configures settings to first check the 'file' handler, then the 'database' handler if not found. ```php public $handlers = ['file', 'database']; ``` -------------------------------- ### Configure ArrayHandler Source: https://github.com/codeigniter4/settings/blob/develop/docs/configuration.md Set up the ArrayHandler with options for class and writeable status. This handler stores settings in memory only. ```php public $array = [ 'class' => ArrayHandler::class, 'writeable' => true, ]; ``` -------------------------------- ### Retrieve Multiple Settings Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use `getMany()` to retrieve an array of configuration values. The returned array is keyed by the exact setting names requested. ```php service('settings')->getMany([ 'App.siteName', 'App.siteEmail', ]); ``` -------------------------------- ### Retrieve Contextual Setting Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Retrieve a setting using its key and a specific context. If the contextual value doesn't exist, it falls back to the general value. ```php $context = 'user:' . user_id(); $theme = service('settings')->get('App.theme', $context); ``` -------------------------------- ### Set a Single Setting Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use the `set()` method to save a configuration value. Boolean values are stored as strings ':true' or ':false', and arrays/objects are serialized. ```php service('settings')->set('App.siteName', 'My Great Site'); ``` -------------------------------- ### Manually Add Settings Namespace to Autoload Config Source: https://github.com/codeigniter4/settings/blob/develop/docs/installation.md After downloading the project manually, add the 'CodeIgniter\Settings' namespace to the \$psr4 array in app/Config/Autoload.php to enable the library. ```php APPPATH, 'Config' => APPPATH . 'Config', 'CodeIgniter\Settings' => APPPATH . 'ThirdParty/settings/src', ]; // ... ``` -------------------------------- ### Set Multiple Contextual Settings Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use `setMany()` with a context parameter to efficiently save multiple settings that are specific to a given context. ```php $context = 'user:' . user_id(); service('settings')->setMany([ 'App.theme' => 'dark', 'App.locale' => 'en', ], $context); ``` -------------------------------- ### Flush All Settings Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use the `flush()` method to completely remove all settings from persistent storage and the in-memory cache. ```php service('settings')->flush(); ``` -------------------------------- ### Set a Configuration Value Source: https://github.com/codeigniter4/settings/blob/develop/docs/index.md Use this to set a configuration value in the database. The value will override the default from the config file. ```php service('settings')->set('App.siteName', 'Example'); ``` -------------------------------- ### Migrate Specific Namespace for CodeIgniter Settings (Windows) Source: https://github.com/codeigniter4/settings/blob/develop/docs/installation.md On Windows, use this command to run migrations specifically for the CodeIgniter\Settings namespace, avoiding migration of other packages. ```bash php spark migrate -n CodeIgniter\Settings ``` -------------------------------- ### Set Contextual Setting Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use the `set()` method with an additional context parameter to store settings specific to a user, environment, or other identifier. ```php $context = 'user:' . user_id(); service('settings')->set('App.theme', 'dark', $context); ``` -------------------------------- ### Default Handlers Configuration Source: https://github.com/codeigniter4/settings/blob/develop/docs/configuration.md Sets the default handler to 'database' for storing and retrieving settings. ```php public $handlers = ['database']; ``` -------------------------------- ### Set Multiple Settings Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use `setMany()` to save multiple key-value pairs efficiently. This method allows supported handlers to persist changes more effectively. ```php service('settings')->setMany([ 'App.siteName' => 'My Great Site', 'App.siteEmail' => 'support@example.com', ]); ``` -------------------------------- ### Database Handler Configuration Source: https://github.com/codeigniter4/settings/blob/develop/docs/configuration.md Configures the DatabaseHandler with specific options for table, connection group, writeability, and deferred writes. ```php public $database = [ 'class' => DatabaseHandler::class, 'table' => 'settings', 'group' => null, 'writeable' => true, 'deferWrites' => false, ]; ``` -------------------------------- ### Migrate Specific Namespace for CodeIgniter Settings (Unix) Source: https://github.com/codeigniter4/settings/blob/develop/docs/installation.md On Unix-based systems, use this command to run migrations specifically for the CodeIgniter\Settings namespace, avoiding migration of other packages. ```bash php spark migrate -n CodeIgniter\\Settings ``` -------------------------------- ### Forget a Configuration Value Source: https://github.com/codeigniter4/settings/blob/develop/docs/index.md Use this to remove a configuration value from the database, reverting to the default value from the config file. ```php service('settings')->forget('App.siteName'); ``` -------------------------------- ### Clearing Settings via Spark Command Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use the `settings:clear` command with the spark CLI to remove all settings from the database. You will be prompted for confirmation before the action is performed. ```bash php spark settings:clear ``` -------------------------------- ### Forget Multiple Settings Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use `forgetMany()` to remove multiple settings from persistent storage simultaneously. ```php service('settings')->forgetMany([ 'App.siteName', 'App.siteEmail', ]); ``` -------------------------------- ### Forget a Single Setting Source: https://github.com/codeigniter4/settings/blob/develop/docs/basic-usage.md Use the `forget()` method to remove a specific setting from persistent storage. This effectively resets the value to its default from the config file, if one exists. ```php service('settings')->forget('App.siteName'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.