certunlp/database-config-bundle
Installation:
composer require unifik/database-config-bundle:dev-master
Update AppKernel.php to include the bundle and extend getContainerBuilder():
public function registerBundles()
{
return [
// ...
new Unifik\DatabaseConfigBundle\UnifikDatabaseConfigBundle(),
];
}
protected function getContainerBuilder()
{
return new \Unifik\DatabaseConfigBundle\DependencyInjection\Compiler\ContainerBuilder(
new \Symfony\Component\DependencyInjection\ParameterBag($this->getKernelParameters())
);
}
Run migrations:
php bin/console doctrine:schema:update --force
First use case:
Define a config in config.yml (e.g., my_config: true) and override it via the database by inserting a record into the unifik_database_config table:
INSERT INTO unifik_database_config (name, value, type)
VALUES ('my_config', 'false', 'parameter');
Dynamic Configuration Management:
config.yml/parameters.yml values without redeploying.# config.yml
features:
experimental_ui: false
-- Override via DB
INSERT INTO unifik_database_config (name, value, type)
VALUES ('features.experimental_ui', 'true', 'parameter');
Environment-Specific Configs:
INSERT INTO unifik_database_config (name, value, type)
VALUES ('database_url', 'mysql://user:pass@prod-db:3306/app', 'parameter');
Admin Panel Integration:
// src/Controller/ConfigController.php
public function update(Config $config, Request $request)
{
$config->setValue($request->request->get('value'));
$entityManager->flush();
return new Response('Config updated!');
}
php bin/console cache:clear
// src/Service/ConfigValidator.php
public function validate(array $configs): array
{
return array_filter($configs, fn($value) => !empty($value));
}
kernel.request to reload configs if needed (e.g., for real-time updates):
// src/EventListener/ConfigReloadListener.php
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->isMasterRequest() && $this->configService->isDirty()) {
$this->configService->reload();
}
}
Cache Invalidation:
Naming Conflicts:
kernel.debug) can break the app.app.my_config) and avoid core keys.Type Mismatches:
$debugMode = (bool) $this->container->getParameter('kernel.debug');
Schema Updates:
doctrine:schema:update --force without backups risks data loss.doctrine:migrations:execute) for safer schema changes.unifik_database_config table to verify configs:
SELECT * FROM unifik_database_config WHERE name LIKE '%my_config%';
Custom Config Sources: Extend the bundle to load configs from other sources (e.g., Redis):
// src/DependencyInjection/Compiler/ExtendedContainerBuilder.php
class ExtendedContainerBuilder extends ContainerBuilder
{
public function __construct(ParameterBag $parameterBag, array $extraSources = [])
{
$this->extraSources = $extraSources;
}
}
Access Control: Add middleware to restrict config updates to admin users:
// src/EventListener/SecureConfigListener.php
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->getRequest()->isXmlHttpRequest() &&
!$this->security->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedException();
}
}
Config Versioning: Track changes to configs using Doctrine lifecycle callbacks:
// src/Entity/Config.php
public function preUpdate()
{
$this->setUpdatedAt(new \DateTime());
}
How can I help you explore Laravel packages today?