Search code examples
symfonyapplication-settings

Symfony2 Store settings in db with caching


Im new in symfony2 framework and i don't know how to store application settings correctly.

What i want:

  • Store my application settings in database (need for admin-center)
  • With caching this setting in the symfony cache system (app/cache/dev|prod)
  • and then get values easy

Is it possible?


Solution

  • You can implement your own resource class implementing the ResourceInterface and also implement your own loader class implementing the LoaderInterface.

    Your loader must load your parameters into a configured ContainerBuilder as the FileLoader and others from the same namespace do.

    Once you are done, you can overwrite the getContainerLoader method of your application Kernel in order to add your loader to the existing ones :

    protected function getContainerLoader(ContainerInterface $container)
    {
        $locator = new FileLocator($this);
        $resolver = new LoaderResolver(array(
            new XmlFileLoader($container, $locator),
            new YamlFileLoader($container, $locator),
            new IniFileLoader($container, $locator),
            new PhpFileLoader($container, $locator),
            new ClosureLoader($container),
            new DatabaseLoader($container),
    
        ));
    
        return new DelegatingLoader($resolver);
    }