PHP code example of olajideolamide / ci4-settings

1. Go to this page and download the library: Download olajideolamide/ci4-settings library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

olajideolamide / ci4-settings example snippets


// app/Config/Autoload.php

public $psr4 = [
    APP_NAMESPACE => APPPATH,
    'Config'      => APPPATH . 'Config',

    // Add this line (keep your existing entries)
    'Jide\Settings' => ROOTPATH . 'vendor/olajideolamide/ci4-settings/src',
];

helper('settings');

public $helpers = ['settings'];

protected $helpers = ['settings'];

// Get 'app.name', return 'My App' if it doesn't exist
$appName = settings('app.name', 'My App');

// Using the helper to get the library instance
settings()->set('app.name', 'My Awesome App');

if (settings()->has('app.name')) {
    // do something
}

settings()->delete('app.name');

$config = settings()->all(); // returns nested array of all settings

// Create or update a feature flag
settings()->set(
    'feature.new_checkout',
    true,                // value
    'checkout',          // optional group
    true                 // is_feature = true
);

if (feature('new_checkout')) {
    // show new checkout flow
} else {
    // fallback to old flow
}

if (feature('feature.new_checkout')) {
    // also works
}

if (feature('beta_banner', false)) {
    // only runs if explicitly enabled
}

settings()->set('mail', [
    'host'       => 'smtp.example.com',
    'port'       => 587,
    'encryption' => 'tls',
    'username'   => '[email protected]',
    'password'   => 'secret',
]);

$host = settings('mail.host');       // "smtp.example.com"
$port = settings('mail.port');       // 587 (int)
$enc  = settings('mail.encryption'); // "tls"

settings()->set('mail', [
    'host' => 'smtp2.example.com',
] + settings('mail', []));

$config = settings()->all(); // or build what you need

// For example, to create a grouped "app" config
settings()->set('app', [
    'name' => 'My App',
    'mail' => [
        'host' => 'smtp.example.com',
    ],
]);

// Later, read:
$host = settings('app.mail.host');

// Get instance
$settings = settings();

// Get value
$appName = settings('app.name', 'My App');

// Chain methods
settings()->set('app.name', 'My App');

use Jide\Settings\Libraries\Settings;

$settings = new Settings(); // uses default config and model

// Jide\Settings\Config\Settings

public string $table    = 'settings';
public bool   $useCache = true;
public int    $cacheTTL = 600;        // seconds
public string $cacheKey = 'ci4_settings';

use Jide\Settings\Config\Settings as SettingsConfig;
use Jide\Settings\Libraries\Settings;

// Create custom config
$config = new SettingsConfig();
$config->useCache = false;
$config->cacheTTL = 0;
$config->table    = 'app_settings'; // if you also customised the migration

$settings = new Settings($config);

use Jide\Settings\Models\SettingModel;

$model = new SettingModel();

$flags = $model
    ->where('is_feature', 1)
    ->orderBy('key', 'ASC')
    ->findAll();
bash
php spark migrate -n Jide\Settings