Symfony Environment


Table of Contents

Configuration Handlers 
Default Configuration Handlers 
Adding Your Own Handler 
How to add custom yaml file in config 
config handler on module level 
Differences between templates and plain .php 
access the helpers from outside the view 
how to use getRequestParameter in partial 
how to use getRequestParameter in partial 
Place for classes, etc 
Place for classes, etc 
Refactoring from action to model 
Refactoring from action to model 

Configuration Handlers 

http://www.symfony-project.org/book/trunk/19-Mastering-Symfony-s-Configuration-Files#Understanding%20Configuration%20Handlers

Each configuration file has a handler. The job of configuration handlers is to manage the configuration cascade, and to do the translation between the configuration files and the optimized PHP code executable at runtime.

Default Configuration Handlers 

The default handler configuration is stored in $sf_symfony_data_dir/config/config_handlers.yml. This file links the handlers to the configuration files according to a file path. Listing 19-7 shows an extract of this file.

Example 1. Listing 19-7 - Extract of $sf_symfony_data_dir/config/config_handlers.yml

config/settings.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: sf_

config/app.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: app_

config/filters.yml:
  class:    sfFilterConfigHandler

modules/*/config/module.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: mod_
    module: yes

For each configuration file (config_handlers.yml identifies each file by a file path with wildcards), the handler class is specified under the class key.

The settings of configuration files handled by sfDefineEnvironmentConfigHandler can be made available directly in the code via the sfConfig class, and the param key contains a prefix value.

$ cat /www/admin_generator/data/symfony/config/module.yml
default:
  enabled:          on
  view_class:       sfPHP
  is_internal:      off
$ cd /www/sfdemo/apps/backend
$ ls -1 config/*.yml
config/app.yml
config/cache.yml
config/factories.yml
config/filters.yml
config/i18n.yml
config/logging.yml
config/routing.yml
config/security.yml
config/settings.yml
config/view.yml
$ ls -1d modules/*
modules/author
modules/category
modules/post
$ cat /www/sfdemo/cache/backend/dev/config/modules_author_config_module.yml.php
<?php
// auto-generated by sfDefineEnvironmentConfigHandler
// date: 2007/01/29 22:33:38
sfConfig::add(array(
  'mod_'.strtolower($moduleName).'_enabled' => true,
  'mod_'.strtolower($moduleName).'_view_class' => 'sfPHP',
  'mod_'.strtolower($moduleName).'_is_internal' => false,
));

Adding Your Own Handler 

Using a handler to deal with a configuration file provides two important benefits:

  • The configuration file is transformed into executable PHP code, and this code is stored in the cache. This means that the configuration is parsed only once in production, and the performance is optimal.
  • The configuration file can be defined at different levels (project and application) and the final parameter values will result from a cascade. So you can define parameters at a project level and override them on a per-application basis.

Using Existing configuration handlers 

If you just need to allow users to retrieve values from the code via sfConfig, you can use the sfDefineEnvironmentConfigHandler configuration handler class.

For instance, to have the url and user parameters available as sfConfig::get('map_url') and sfConfig::get('map_user'), You may want to store these two parameters in a custom configuration file called map.yml, located in the application config/ directory. This configuration file might contain the following:

api:
  url:  map.api.example.com
  user: foobar

And define your handler as follows:

config/map.yml:
  class: sfDefineEnvironmentConfigHandler
  param:
    prefix: map_

Be careful not to choose a prefix already used by another handler. Existing prefixes are sf_, app_, and mod_.

When you need the code based on the map.yml file and generated by the myMapConfigHandler handler in your application, call the following line:

include(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/map.yml'));

When calling the checkConfig() method, symfony looks for existing map.yml files in the configuration directories and processes them with the handler specified in the config_handlers.yml file.

How to add custom yaml file in config 

http://www.symfony-project.org/forum/index.php?t=msg&goto=31504

> I mean a custom yml file in /config that can be parsed and cached just like
> app.yml.

I added test.yml in /myapp/config/ and changed the config_handlers.php like the following:

config/test.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: test_

(I missed ":" in the test.yml at first)

I added one line in myapp/config/config.php

require_once(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/test.yml'));

It works!

documented on: 16 July 2007, cysin

config handler on module level 

http://www.symfony-project.org/forum/index.php?t=msg&goto=28830

I'm using set of yml files for an additional config, all handled by sfDefineEnvironmentConfigHandler.

my config_handlers.yml:

config/my_settings.yml:
  class:  sfDefineEnvironmentConfigHandler
  param:
    prefix: my_glob_settings_

modules/*/config/my_settings.yml:
  class:  sfDefineEnvironmentConfigHandler
  param:
    prefix: my_mod_settings_
    module: yes

The config/my_settings.yml file is being parsed and cached nicely, however I cannot get it to work on the module level.

Problem solved, caused by a missing slash in function returning full path to the module's yml file