How to create a custom yaml config file in Symfony

前端 未结 6 469
面向向阳花
面向向阳花 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 13:52

    if your cached config-file is empty, you have probably forgotten to set the environment in your yml-file.

    like:

    all:
      test:  value1
      test2: value2
    
    dev:
      test2: value3
    
    0 讨论(0)
  • 2021-02-02 13:55

    It's really easy, but also a little bit hacky:

    Create the file /config/config_handlers.yml and add this:

    config/something.yml:
      class:    sfDefineEnvironmentConfigHandler
      param:
        prefix: something_
    

    Then add these two lines to /web/index.php after ... getApplicationConfiguration() (and also add them to frontend_dev.php and wherever you want this config file to be available):

    $configCache = new sfConfigCache($configuration);
    include($configCache->checkConfig('config/something.yml'));
    

    So your /web/index.php might look like this afterwards:

    <?php
    require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
    
    $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
    $configCache = new sfConfigCache($configuration);
    $configCache->checkConfig('config/something.yml');
    sfContext::createInstance($configuration)->dispatch();
    

    Btw: This is also in the documentation you cited, although the checkConfig() call is in a different place. Look for this: "When you need the code based on the map.yml file and generated by the myMapConfigHandler handler in your application, call the following line:"

    Have fun ;-)

    0 讨论(0)
  • 2021-02-02 13:57

    Do not modify the index.php this is dirty!

    Juste add this line to your app/frontend/config/frontendConfiguration.class.php

    require_once($this->getConfigCache()->checkConfig('config/something.yml'));
    

    (adapt with your own app name)

    0 讨论(0)
  • 2021-02-02 14:02

    Have you cleared your cache files?

    php symfony cc
    

    In prodution environment all config files, classes, etc... are being cached.

    0 讨论(0)
  • 2021-02-02 14:14

    Works in all application files:

    $configCache = sfApplicationConfiguration::getActive()->getConfigCache();
    $configCache->registerConfigHandler('config/something.yml', 'sfDefineEnvironmentConfigHandler', Array('prefix' => 'something_'));
    include $configCache->checkConfig('config/something.yml');
    

    Then you can use:

    sfConfig::get("something_foo");
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题