18

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 config directory. It looks like that:

prod:
  test:  ok

dev:
  test: ko

all:
  foo:  bar
  john: doe

Then I copied the config_handlers.yml and also put it in the config directory and added the following at the top of the file:

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

But if I'm calling sfConfig::get("something_foo"); I keep getting NULL.

What did I do wrong? I just want to read values, so no need to create a custome config handler, right?

I've read the doc here: http://www.symfony-project.org/book/1_2/19-Mastering-Symfony-s-Configuration-Files even though I'm running 1.4 (I don't think that changed since then).

Edit: Of course I can use sfYaml::load() but I'd like to do things in a better way.

Guillaume Flandre
  • 8,624
  • 8
  • 43
  • 53
  • Out of curiosity, what would you use the custom config file for that's different from the functionality that existing config files already offer? If it's just simple data, what's wrong with app.yml? or a custom class in /lib/? – Tom Mar 30 '10 at 15:32
  • app.yml would work fine. But we're talking about a lot of data here. I would just like to keep things separated. – Guillaume Flandre Mar 30 '10 at 16:03
  • I see.... the YAML format is quite anal to maintain, so personally I wouldn't go down this route at all for storing data but I suppose you have your reasons. – Tom Mar 30 '10 at 18:09
  • @Tom: Yaml files rock, just glorified arrays. I love XML as well, but IMO Yaml files are far cleaner and easier to work with when using them as configuration setups. – Mike Purcell Feb 21 '12 at 21:42

6 Answers6

12

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)

Syam
  • 151
  • 1
  • 4
5

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

naag
  • 263
  • 1
  • 7
5

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

wdev
  • 2,094
  • 19
  • 26
  • I'm sure that this is a nice way to include (custom) yml-files, though I have a `sfProjectConfiguration` in stead of a `sfApplicationConfiguration`. Do you happen to know how to include it in that way? – LaVomit Nov 10 '15 at 11:07
1

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");
ЯegDwight
  • 23,615
  • 10
  • 43
  • 51
sredni
  • 213
  • 2
  • 9
1

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
Thorsten
  • 11
  • 1
0

Have you cleared your cache files?

php symfony cc

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

martin
  • 76,615
  • 21
  • 156
  • 193