Installation:
composer require dhorchler/config-bundle:dev-master
Update AppKernel.php to include the bundle:
new DHorchler\ConfigBundle\DHorchlerConfigBundle(),
Database Setup:
Run migrations to create the config table:
php app/console doctrine:migrations:diff
php app/console doctrine:migrations:migrate
Configure Sonata Admin:
Add the bundle to sonata_block configuration in config.yml:
sonata_block:
blocks:
dhorchler_config.admin_block_config:
contexts: [admin]
First Use Case: Define a config entry via Sonata Admin UI:
/admin/config).site_name, type string).$siteName = $this->get('dhorchler_config.manager')->get('site_name');
Defining Configs: Use Sonata Admin to create configs with:
max_upload_size).string, integer, choice, etc.).Retrieving Values:
Inject the DHorchler\ConfigBundle\Manager\ConfigManager service:
$value = $this->get('dhorchler_config.manager')->get('key');
Use in templates:
{{ app.config.get('key') }}
Updating Values:
$this->get('dhorchler_config.manager')->set('key', 'new_value');
Validation:
Define rules in Sonata Admin (e.g., min/max for integer types). Errors are customizable via YAML:
constraints:
- NotBlank: { message: "This value is required" }
- Range: { min: 1, max: 100, message: "Must be between 1 and 100" }
Default Values:
Set defaults in the Sonata Admin form. Override globally in config.yml:
dhorchler_config:
defaults:
site_name: "My App"
$value = $this->get('dhorchler_config.manager')->get('key', ['cache' => true]);
# app/config/parameters.yml
parameters:
dhorchler_config.seed:
site_name: "%env(SITE_NAME)%"
$dispatcher->addListener('dhorchler_config.update', function() {
$this->get('cache')->clear();
});
Bundle Maturity:
spatie/laravel-settings (database-backed configs with caching).beberlei/doctrineextensions + custom admin panel.Sonata Admin Dependency:
config() system or packages like spatie/laravel-settings.Migration Issues:
doctrine:migrations:diff step may fail if the config table already exists. Manually check the schema:
php app/console doctrine:schema:update --dump-sql
Type-Specific Quirks:
DateType/DateTimeType; ensure your Doctrine entities are compatible.Caching Conflicts:
$this->get('dhorchler_config.manager')->set('key', 'value', ['cache_tag' => 'config']);
Missing Configs:
config table). Use:
php app/console doctrine:query:sql "SELECT * FROM config"
Validation Errors:
Permission Issues:
Config admin class. Check sonata_admin.security.handler in config.yml.Custom Data Types:
Extend the bundle by creating a new Type class (e.g., BooleanType) and register it in the bundle’s services:
# DHorchlerConfigBundle/Resources/config/services.yml
services:
dhorchler_config.type.boolean:
class: AppBundle\Type\BooleanType
tags:
- { name: dhorchler_config.type }
Custom Admin Fields:
Override the Sonata Admin form type for the Config entity:
// src/AppBundle/Admin/ConfigAdmin.php
class ConfigAdmin extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('value', 'your_custom_field_type');
}
}
Event Listeners: Subscribe to config update events to trigger side effects (e.g., log changes):
// src/AppBundle/EventListener/ConfigListener.php
class ConfigListener
{
public function onConfigUpdate(ConfigUpdateEvent $event)
{
\Log::info("Config updated: {$event->getKey()}");
}
}
Register the listener in services.yml:
services:
app.config_listener:
class: AppBundle\EventListener\ConfigListener
tags:
- { name: kernel.event_listener, event: dhorchler_config.update }
Laravel Adaptation: If you must use this in Laravel, create a facade to wrap the Symfony manager:
// app/Facades/ConfigFacade.php
class ConfigFacade extends Facade
{
protected static function getFacadeAccessor() { return 'dhorchler_config.manager'; }
}
Register the Symfony container as a Laravel service provider (advanced).
How can I help you explore Laravel packages today?