Installation Add the bundle via Composer:
composer require bluesteel42/settings-bundle
Register the bundle in AppKernel.php:
$bundles[] = new BlueSteel42\SettingsBundle\BlueSteel42SettingsBundle();
First Use Case
Define a setting in a YAML file (default backend) under %kernel.root_dir%/Resources/config/settings.yml:
app:
debug: true
max_retries: 3
Access settings in a controller or service:
$settings = $this->get('bluesteel42_settings.settings');
$debugMode = $settings->get('app/debug'); // Returns `true`
Where to Look First
config.yml for backend selection.%kernel.root_dir%/Resources/config/settings.yml (default).bluesteel42_settings.settings for runtime access.Defining Settings Use YAML/XML files or Doctrine DBAL for structured configuration:
# config/settings.yml
features:
notifications: true
analytics: false
Access via:
$this->get('bluesteel42_settings.settings')->get('features/notifications');
Dynamic Backend Switching
Override the backend in config.yml for different environments:
# config_prod.yml
bluesteel42_settings:
backend: doctrinedbal
table_name: app_settings
Validation and Defaults Set defaults in code or config files:
$settings->set('app/timeout', 30, ['default' => 60]); // Falls back to 60
Integration with Services
Inject the Settings service into controllers/services:
use BlueSteel42\SettingsBundle\Settings\SettingsInterface;
class MyService {
public function __construct(SettingsInterface $settings) {
$this->settings = $settings;
}
}
Environment-Specific Overrides
Use separate config files (e.g., settings_dev.yml, settings_prod.yml) and merge them:
# config.yml
bluesteel42_settings:
files:
- "%kernel.root_dir%/config/settings.yml"
- "%kernel.root_dir%/config/settings_%kernel.environment%.yml"
File Permissions
Ensure the storage directory (e.g., Resources/config/) is writable by the web server:
chmod -R 775 %kernel.root_dir%/Resources/config/
Doctrine DBAL Schema
If using doctrinedbal, manually create the table (no migrations are provided):
CREATE TABLE app_settings (
key VARCHAR(255) PRIMARY KEY,
value TEXT,
type VARCHAR(50)
);
Caching Quirks Changes to YAML/XML files require a cache clear to reflect:
php bin/console cache:clear
Key Collisions Avoid duplicate keys across files; later files overwrite earlier ones.
bluesteel42_settings events.php bin/console debug:config bluesteel42_settings to verify settings.Custom Backends
Implement BlueSteel42\SettingsBundle\Settings\Backend\BackendInterface for new storage (e.g., Redis):
class RedisBackend implements BackendInterface {
public function load() { /* ... */ }
public function save($data) { /* ... */ }
}
Register via config.yml:
bluesteel42_settings:
backend: redis
Event Listeners
Subscribe to settings.load and settings.save events for pre/post-processing:
services:
app.settings_listener:
class: AppBundle\EventListener\SettingsListener
tags:
- { name: kernel.event_listener, event: settings.load, method: onLoad }
Type Safety Enforce types in settings (e.g., booleans, integers) via validation rules:
$settings->set('app/enabled', true, ['type' => 'bool']);
How can I help you explore Laravel packages today?