dolmitos/symfony-settings-bundle
Installation
composer require dolmitos/symfony-settings-bundle
Register the bundle in config/bundles.php:
Dolmitos\SettingsBundle\DolmitosSettingsBundle::class => ['all' => true],
First Configuration
Define settings in config/packages/dolmitos_settings.yaml:
dolmitos_settings:
settings:
app:
title: "My App"
debug: "%kernel.debug%"
features:
new_ui: true
Accessing Settings
Inject the Settings service in a controller or service:
use Dolmitos\SettingsBundle\Settings\SettingsInterface;
public function __construct(private SettingsInterface $settings) {}
public function index()
{
$title = $this->settings->get('app.title');
// Use $title in your logic
}
Environment Overrides
Override settings in .env:
DOLMITOS_SETTINGS_APP_TITLE="Custom Title"
DOLMITOS_SETTINGS_APP_DEBUG=false
Use nested YAML for hierarchical settings:
dolmitos_settings:
settings:
api:
endpoints:
v1: "https://api.example.com/v1"
v2: "https://api.example.com/v2"
enabled: true
Access via:
$this->settings->get('api.endpoints.v1'); // Returns "https://api.example.com/v1"
Leverage .env for runtime flexibility:
# config/packages/dolmitos_settings.yaml
dolmitos_settings:
settings:
cache:
driver: "%env(DOLMITOS_SETTINGS_CACHE_DRIVER)%"
ttl: "%env(int:DOLMITOS_SETTINGS_CACHE_TTL)%"
Override in .env:
DOLMITOS_SETTINGS_CACHE_DRIVER="redis"
DOLMITOS_SETTINGS_CACHE_TTL=3600
Define validation in YAML:
dolmitos_settings:
settings:
mail:
from: "noreply@example.com"
smtp:
host: "smtp.example.com"
port: 587
validation:
mail:
from: "email"
smtp:
host: "required|string"
port: "required|integer|min:1|max:65535"
Trigger validation via:
$this->settings->validate();
Enable caching for performance:
dolmitos_settings:
cache: true # Uses Symfony cache system
cache_key: "app_settings" # Optional custom cache key
Clear cache when settings change:
$this->settings->clearCache();
Bind settings to forms for admin panels:
use Dolmitos\SettingsBundle\Form\SettingsType;
$form = $this->createForm(SettingsType::class, $this->settings->getAll());
Circular Dependencies
Avoid injecting SettingsInterface in the bundle’s own services to prevent circular references.
Cache Invalidation
Forgetting to clear the cache (php bin/console cache:clear) after modifying settings in YAML/ENV may lead to stale values.
Validation Errors Silent failures occur if validation rules are malformed. Always check logs or use:
$errors = $this->settings->validate();
if ($errors) {
// Handle errors
}
Dump All Settings Use a twig template or controller to debug:
dd($this->settings->getAll());
Environment-Specific Overrides
Use %kernel.environment% in YAML to conditionally load settings:
dolmitos_settings:
settings:
debug_bar: "%kernel.debug%"
Parameter Precedence
Order of precedence (highest to lowest):
.env.local > .env > config/packages/dolmitos_settings.yaml.
Custom Validators
Implement Dolmitos\SettingsBundle\Validator\ConstraintValidatorInterface for bespoke validation.
Event Listeners
Subscribe to SettingsLoadedEvent to react to setting changes:
use Dolmitos\SettingsBundle\Event\SettingsLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
SettingsLoadedEvent::NAME => 'onSettingsLoaded',
];
}
public function onSettingsLoaded(SettingsLoadedEvent $event)
{
// Logic here
}
}
Database Backend
While not natively supported, extend the bundle by implementing a custom SettingsProvider to fetch settings from a database.
How can I help you explore Laravel packages today?