1. Go to this page and download the library: Download salehye/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/ */
// Get a setting
$siteName = settings('site_name');
$siteName = setting('site_name'); // Alias
// Set a setting
set_setting('site_name', 'My Website');
// Get group
$general = settings_group('general');
// Get public settings
$public = settings_public();
// Get all settings
$all = settings_all();
// With default value
$timezone = settings('timezone', 'UTC');
use Salehye\Settings\Facades\Settings;
// Get
$value = Settings::get('site_name');
$value = Settings::get('site_name', 'Default');
// Set
Settings::set('site_name', 'My Website');
// Multiple
$settings = Settings::getMany(['site_name', 'timezone']);
Settings::setMany([
'site_name' => 'My Site',
'timezone' => 'Asia/Riyadh',
]);
// Group
$general = Settings::group('general');
// Public
$public = Settings::public();
// All
$all = Settings::all();
// Check
if (Settings::has('site_name')) {
// ...
}
// Delete
Settings::delete('site_name');
// Cache
Settings::clearCache();
Settings::reload();
use Salehye\Settings\Facades\Settings;
// Magic getter
$siteName = Settings::$site_name;
// Magic isset
if (isset(Settings::$site_name)) {
// ...
}
// Invoke as function
$value = Settings('site_name');
$value = Settings('site_name', 'default');
$all = Settings(); // Returns all settings
use Salehye\Settings\Concerns\HasSettings;
class MyController extends Controller
{
use HasSettings;
public function index()
{
$siteName = $this->getSetting('site_name');
$this->setSetting('timezone', 'UTC');
$settings = $this->getSettings(['site_name', 'timezone']);
$general = $this->getSettingsGroup('general');
$public = $this->getPublicSettings();
$all = $this->getAllSettings();
if ($this->hasSetting('site_name')) {
// ...
}
$this->deleteSetting('old_setting');
$this->clearSettingsCache();
}
}
use Salehye\Settings\SettingsManager;
use Salehye\Settings\Contracts\SettingsRepositoryInterface;
class MyService
{
public function __construct(
protected SettingsManager $settings,
// OR
protected SettingsRepositoryInterface $repository
) {}
public function doSomething(): void
{
$value = $this->settings->get('site_name');
// OR
$value = $this->repository->get('site_name');
}
}
use Salehye\Settings\Contracts\SettingsRepositoryInterface;
class MyService
{
public function __construct(
protected SettingsRepositoryInterface $settings
) {}
public function doSomething(): void
{
$value = $this->settings->get('my_setting');
}
}
// Clear cache after bulk updates
Settings::clearCache();
// Reload from database
Settings::reload();