How to create a custom yaml config file in Symfony

前端 未结 6 473
面向向阳花
面向向阳花 2021-02-02 13:52

What I want to do is quite simple: store data in a custom config file that I want to read later on.

I created my file something.yml that I put in the global

6条回答
  •  执念已碎
    2021-02-02 14:16

    If you're doing this for a plugin you need to load the configuration file in the initialize() method. You can still use config_handlers.yml in your plugin's config directory or let the plugin load the handler too.

    class myPluginConfiguration extends sfPluginConfiguration
    {
      public function setup() // loads handler if needed
      {
        if ($this->configuration instanceof sfApplicationConfiguration)
        {
          $configCache = $this->configuration->getConfigCache();
          $configCache->registerConfigHandler('config/features.yml', 'sfDefineEnvironmentConfigHandler',
            array('prefix' => 'feature_'));
          $configCache->checkConfig('config/features.yml');
        }
      }
    
      public function initialize() // loads the actual config file
      {
        if ($this->configuration instanceof sfApplicationConfiguration)
        {
          $configCache = $this->configuration->getConfigCache();
          include($configCache->checkConfig('config/features.yml'));
        }
      }
    }
    

    The plugin's config initialize() method is called automatically by sfProjectConfiguration class and all appConfiguration classes (trough inheritance).

提交回复
热议问题