How to create a custom yaml config file in Symfony

守給你的承諾、 提交于 2019-12-02 20:37:36

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)

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 ;-)

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).

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
sredni

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");

Have you cleared your cache files?

php symfony cc

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!