In this article we assume that you are familiar with The YAML Format and Configuration File Principles.
Factories are core objects needed by the framework during the life of any request. They are configured in the factories.yml configuration file and always accessible via the sfContext object:
// get the user factory sfContext::getInstance()->getUser();
The main factories.yml configuration file for an application can be found in the apps/APP_NAME/config/ directory.
# General configuration env_name: factory_name: class: class_name file: absolute_path_to_file param: array_of_parameters env_name: # Mailer factory configuration mailer: class: sfMailer param: logging: <true | false> charset: charset_name delivery_strategy: <realtime | single_address | spool | none> delivery_address: email spool_class: <Swift_FileSpool | Swift_DoctrineSpool | Swift_PropelSpool> spool_arguments: transport: class: <Swift_SmtpTransport | Swift_SendmailTransport | Swift_MailTransport | Swift_NullTransport> param: # Swift_SmtpTransport class host: smtp_host port: smtp_port encryption: <ssl | tls> username: username password: password # Swift_SendmailTransport class command: sendmail_command # Request factory configuration request: class: sfWebRequest param: logging: <true | false> path_info_array: global_php_array path_info_key: key_name relative_url_root: part_of_the_url default_format: format_name formats: file_extension: content_types # Response factory configuration response: class: sfWebResponse param: logging: <true | false> send_http_headers: <true | false> http_protocol: http_protocol_version charset: charset_name content_type: content_type_name # User factory configuration user: class: myUser param: logging: <true | false> auto_shutdown: <true | false> default_culture: culture_name use_flash: <true | false> # sfBasicSecurityUser class timeout: timeout_in_seconds # Storage factory configuration storage: class: class_name param: auto_shutdown: <true | false> # sfSessionStorage class session_name: cookie_name session_id: session_id_value auto_start: <true | false> session_cookie_lifetime: lifetime_in_seconds session_cookie_path: cookie_path session_cookie_domain: cookie_domain session_cookie_secure: <true | false> session_cookie_httponly: <true | false> session_cache_limiter: <nocache | private | private_no_expire | public> # sfDatabaseSessionStorage class database: database_name db_table: table_name db_id_col: session_id_column db_data_col: session_data_column db_time_col: session_time_column # sfCacheSessionStorage class cache: anonymous_cache_factory # sfSessionTestStorage class session_path: session_path_string # View cache manager factory configuration view_cache_manager: class: sfViewCacheManager param: cache_key_use_vary_headers: <true | false> cache_key_use_host_name: <true | false> # View cache factory configuration view_cache: class: class_name param: lifetime: lifetime_in_seconds automatic_cleaning_factor: integer_value prefix: cash_key_prefix # sfFileCache class cache_dir: path_to_directory # sfSQLiteCache class database: sqlite_db_name # sfMemcacheCache class host: memcache_host port: memcache_port persistent: <true | false> servers: array_of_servers # I18n factory configuration i18n: class: sfI18N param: source: <XLIFF | SQLite | MySQL | gettext> debug: <true | false> untranslated_prefix: prefix_string untranslated_suffix: suffix_string cache: anonymous_cache_factory # Routing factory configuration routing: class: sfPatternRouting param: logging: <true | false> variable_prefixes: [prefix1, prefix2, ...] segment_separators: [seperator1, seperator2, ...] generate_shortest_url: <true | false> extra_parameters_as_query_string: <true | false> cache: anonymous_cache_factory suffix: suffix_string load_configuration: <true | false> lazy_routes_deserialize: <true | false> lookup_cache_dedicated_keys: <true | false> variable_regex: regular_expression debug: <true | false> default_module: module_name default_action: action_name # Logger factory configuration logger: class: class_name param: level: <emerg | alert | crit | err | warning | notice | info | debug> # sfWebDebugLogger class web_debug_class: class_name # sfFileLogger class file: file_path format: log_line_format type: type_string time_format: strftime_format dir_mode: dir_mode file_mode: file_mode # sfVarLogger class xdebug_logging: <true | false> # sfStreamLogger class stream: php_stream # sfAggregateLogger class loggers: array_of_loggers # Controller factory configuration controller: class: sfFrontWebController
The factories.yml file is environment-aware, therefore you can add configurations for different environments by specifying environment name (eg.: prod, test) or 'all' for all environments.
env_name > factory_name
Defines a factory name. The factories.yml configuration file contains a list of named factories:
FACTORY_1: # definition of factory 1 FACTORY_2: # definition of factory 2 # ...
The supported factory names are: controller, logger, i18n, request, response, routing, storage, user, view_cache, and view_cache_manager.
env_name > factory_name > class
Defines a factory class, which is used to create factory object.
When the sfContext initializes the factories, it reads the factories.yml file for the class name of the factory (class) and the parameters (see param option) used to configure the factory object. Being able to customize the factories means that you can use a custom class for symfony core objects instead of the default one. You can also change the default behavior of these classes by customizing the parameters sent to them.
env_name > factory_name > file
Defines an absolute path to a class file. If the factory class cannot be autoloaded, a file path can be defined and will be automatically included before the factory is created.
env_name > factory_name > param
Defines an array of options used to configure factory object. These options are passed as an array to the constructor of a factory class.
env_name > mailer
This factory is used to configure mailer core object. sfContext Accessor: $context→getMailer().
env_name > mailer > class
sfMailer class is the main entry point for the mailer system. Default: sfMailer
env_name > mailer > param > logging
Specifies whether to enable logging or not. By default, it uses the logging_enabled setting from settings.yml. Possible values: <true | false> Default: %SF_LOGGING_ENABLED%
env_name > mailer > param > charset
Defines the charset to use for the mail messages. By default, it uses the charset setting from settings.yml. Default: %SF_CHARSET%
env_name > mailer > param > delivery_strategy
Defines how email messages are delivered by the mailer. Four strategies are available by default, which should suit all the common needs:
Possible values: <realtime | single_address | spool | none>
env_name > mailer > param > delivery_address
Defines the recipient of all message when the delivery_strategy is set to single_address.
env_name > mailer > param > spool_class
Defines the spool class to use when the delivery_strategy is set to spool:
When the spool is instantiated, the spool_arguments option is used as the constructor arguments.
Defines the constructor arguments of the spool. Here are the options available for the built-in queues classes: Swift_FileSpool:
Swift_DoctrineSpool:
Swift_PropelSpool:
The configuration below shows a typical configuration for a Doctrine spool:
mailer: class: sfMailer param: delivery_strategy: spool spool_class: Swift_DoctrineSpool spool_arguments: [ MailMessage, message, getSpooledMessages ]
Defines the transport for the mailer. A transport is the component which actually does the sending.
Defines the transport class. The class setting can be any class that implements from Swift_Transport, and four are provided by default:
Default: Swift_SmtpTransport
Defines an array of parameters to configure the transport. The «Transport Types» section of the Swift Mailer official documentation describes all you need to know about the built-in transport classes and their different parameters. The default configuration:
mailer: class: sfMailer param: ... transport: class: Swift_SmtpTransport param: host: localhost port: 25 encryption: ~ username: ~ password: ~
<h4 class="sectionheader">Swift_SmtpTransport class</h4>
Specifies SMTP server host.
Specifies SMTP server port. Default: 25
Specifies the encryption used to connect to SMTP server. Note: For SSL or TLS encryption to work your PHP installation must have appropriate OpenSSL transports wrappers. You can check if «tls» and/or «ssl» are present in your PHP installation by using the PHP function stream_get_transports(). Possible values: <ssl | tls>
Specifies the username used to connect to SMTP server.
Specifies the password used to connect to SMTP server.
<h4 class="sectionheader">Swift_SendmailTransport class</h4>
Specifies the command used to call sendmail. Default: /usr/sbin/sendmail -bs
This factory is used to configure request core object. sfContext Accessor: $context→getRequest().
Defines PHP class used to create http request instance. Must extend sfRequest abstract class. sfWebRequest class provides methods for manipulating client request information such as attributes, and parameters. It parses input from the request and store them as parameters. It is also possible to manipulate the request method originally sent by the user. Default: sfWebRequest
Specifies whether to enable logging or not. By default, it uses the logging_enabled setting from settings.yml. Possible values: <true | false> Default: %SF_LOGGING_ENABLED%
Defines the global PHP array that will be used to retrieve information. On some configurations you may want to change the default SERVER value to ENV. Default: SERVER
Defines the key under which the PATH_INFO information can be found. If you use IIS with a rewriting module like IIFR or ISAPI, you may need to change this value to HTTP_X_REWRITE_URL. Default: PATH_INFO
Defines the part of the URL before the front controller. Most of the time, this is automatically detected by the framework and does not need to be changed.
Defines the default format (file extension) to use for the response. The available formats are defined in the formats option below.
Defines an array of file extensions and their corresponding Content-Types. It is used by the framework to automatically manage the Content-Type of the response, based on the request URI extension.
Defines file extension and its corresponding Content-Type. By default there are preconfigured some of the formats:
request: class: sfWebRequest param: ... formats: txt: text/plain js: [application/javascript, application/x-javascript, text/javascript] css: text/css json: [application/json, application/x-json] xml: [text/xml, application/xml, application/x-xml] rdf: application/rdf+xml atom: application/atom+xml
This factory is used to configure response core object. sfContext Accessor: $context→getResponse().
Defines PHP class used to create http response instance. Must extend sfResponse abstract class. sfWebResponse class manages web reponses and provides methods for manipulating client response information such as headers, cookies and content. Default: sfWebResponse
Specifies whether to enable logging or not. By default, it uses the logging_enabled setting from settings.yml. Possible values: <true | false> Default: %SF_LOGGING_ENABLED%
Specifies whether the response should send HTTP response headers along with the response content. This setting is mostly useful for testing, as headers are sent with the header() PHP function which sends warnings if you try to send headers after some output. Possible values: <true | false> Default: true
Defines the HTTP protocol version to use for the response. By default, it checks the $_SERVER['SERVER_PROTOCOL'] value if available or defaults to HTTP/1.0. Default: HTTP/1.0
Defines the charset to use for the response. By default, it uses the charset setting from settings.yml, which is what you want most of the time. Default: %SF_CHARSET%
Defines the content type. Default: text/html
This factory is used to configure user core object. sfContext Accessor: $context→getUser().
Defines PHP class used to create user instance. Must extend sfUser class. Built-in user classes:
By default, the default myUser class inherits from sfBasicSecurityUser, which can be configured in the security.yml configuration file. Default: myUser
Defines whether to enable logging. This option is available for all user classes. Possible values: <true | false> Default: false
Defines whether to automatically save the changes to the session. This option is available for all user classes. Possible values: <true | false> Default: true
Defines the default culture to use for a user who comes to the site for the first time. By default, it uses the default_culture setting from settings.yml, which is what you want most of the time. This option is available for all user classes. Default: %SF_DEFAULT_CULTURE%
Defines whether to enable the flash component. This option is available for all user classes. Possible values: <true | false> Default: false
<h4 class="sectionheader">sfBasicSecurityUser class</h4>
Defines the timeout for user authentication. It is not related to the session timeout. The default setting automatically unauthenticates a user after 30 minutes of inactivity. This setting is only used by user classes that inherit from the sfBasicSecurityUser base class, which is the case of the generated myUser class. Default: 1800
This factory is used to configure storage core object. sfContext Accessor: $context→getStorage(). The storage factory is used by the user factory to persist user data between HTTP requests.
Defines PHP class used to create storage instance. Must extend sfStorage abstract class which allows you to customize the way symfony stores its persistent data. Built-in storage classes:
Default: sfSessionStorage
Defines whether to automatically save the changes to the session. This option is available for all storage classes. Possible values: <true | false> Default: true
<h4 class="sectionheader">sfSessionStorage class</h4>
Defines the name of the cookie used by symfony to store the user session. By default, the name is symfony, which means that all your applications share the same cookie (and as such the corresponding authentication and authorizations). This option is available for sfSessionStorage, sfCacheSessionStorage and database storage classes. Default: symfony
Defines the session identifier. This option is available for sfSessionStorage, sfSessionTestStorage and database storage classes. If you define this option for sfSessionStorage or database storage classes, session_id value will be assigned to a session only if session.use_cookies setting in your php.ini is set to false. Default: null
Enables or disables the session auto-starting feature of PHP (via the session_start() function). This option is available only for sfSessionStorage and database storage classes. Possible values: <true | false> Default: true
Specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means «until the browser is closed.» Note that the expiration timestamp is set relative to the server time, which is not necessarily the same as the time in the client's browser. This option is available for sfSessionStorage, sfCacheSessionStorage and database storage classes. Default: 0
Specifies path to set in the session cookie. This option is available for sfSessionStorage, sfCacheSessionStorage and database storage classes. Default: /
Specifies the domain to set in the session cookie. Default is none at all meaning the host name of the server which generated the cookie according to cookies specification. This option is available for sfSessionStorage, sfCacheSessionStorage and database storage classes.
Specifies whether cookies should only be sent over secure connections. This option is available for sfSessionStorage, sfCacheSessionStorage and database storage classes. Default: false
Marks the cookie as accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript. This setting can effectively help to reduce identity theft through XSS attacks (although it is not supported by all browsers). This option is available for sfSessionStorage, sfCacheSessionStorage and database storage classes. Possible values: <true | false> Default: false
Specifies the cache control method used for session pages. See also the session_cache_limiter() documentation for information about what available values mean. If the session_cache_limiter option is set to null (which is the default), the session cache limiter value will be set by PHP automatically according to the php.ini configuration file. For all other values, PHP's session_cache_limiter() function is called and the option value is passed as an argument. This option is available only for sfSessionStorage and database storage classes. Default: null
<h4 class="sectionheader">sfDatabaseSessionStorage class</h4>
Defiens the name of database in which session data will be stored. This option is available for database storage classes which extends sfDatabaseSessionStorage abstract class.
Defiens the database table in which session data will be stored. This option is available for database storage classes which extends sfDatabaseSessionStorage abstract class.
Defines the database column in which the session id will be stored. This option is available for database storage classes which extends sfDatabaseSessionStorage abstract class. Default: sess_id
Defines the database column in which the session data will be stored. This option is available for database storage classes which extends sfDatabaseSessionStorage abstract class. Default: sess_data
Defines the database column in which the session timestamp will be stored. This option is available for database storage classes which extends sfDatabaseSessionStorage abstract class. Default: sess_time
<h4 class="sectionheader">sfCacheSessionStorage class</h4>
Defines an anonymous cache factory to be used for caching session data (see the view_cache factory for more information), eg.:
storage: class: sfCacheSessionStorage param: ... cache: class: sfFileCache param: automatic_cleaning_factor: 0 cache_dir: %SF_I18N_CACHE_DIR% lifetime: 31556926 prefix: %SF_APP_DIR%/i18n
This option is available only for sfCacheSessionStorage class.
<h4 class="sectionheader">sfSessionTestStorage class</h4>
Defines the path to store the session files. This option is available only for sfSessionTestStorage class.
This factory is used to configure view cache manager core object. sfContext Accessor: $context→getViewCacheManager(). This factory is only created if the cache setting in settings.yml file is set to true. Most configuration of this factory is done via the view_cache factory, which defines the underlying cache object used by the view cache manager.
sfViewCacheManager is a cache class to cache the HTML results for actions and templates. This class uses a sfCache instance implementation to store cache. Default: sfViewCacheManager
Specifies if the cache keys should include the vary headers part. In practice, it says if the page cache should be HTTP header dependent, as specified in vary cache parameter. Possible values: <true | false> Default: true
Specifies if the cache keys should include the host name part. In practice, it says if page cache should be hostname dependent. Possible values: <true | false> Default: true
This factory is used to configure view cache core object. It does not have sfContext any accessor, it is used directly by the view_cache_manager factory. This factory is only defined if the cache setting in settings.yml file is set to true. The view_cache factory defines a cache class that must inherit from sfCache. <strong>Anonymous Cache Factories</strong>
Several factories (view_cache, i18n, and routing) can take advantage of a cache object if defined in their configuration. The configuration of the cache object is similar for all factories. The cache key defines an anonymous cache factory. Like any other factory, it takes a class and a param entries. The param entry can take any option available for the given cache class.
Defines PHP class used to create view cache object instance. Must extend sfCache abstract class. Built-in logger classes:
Default: sfFileCache
Defines the default life time in seconds. This option is available for all cache drivers. Default: 86400
The automatic cleaning process destroy too old (for the given life time) cache files when a new cache file is written.
This option is available for all cache drivers. Default: 1000
Defines a prefix added to every cache key. It allows to share or separate a cache between different environments/applications/projects. Use the environment in the prefix to use different cache depending on the environment. Use the same prefix for two applications if you want their cache to be shared. This option is available for all cache drivers.
<h4 class="sectionheader">sfFileCache class</h4>
Defines the directory where to put cache files. This option is available only for sfFileCache class.
<h4 class="sectionheader">sfSQLiteCache class</h4>
Defines the file where to put the cache database (or :memory: to store cache in memory). This option is available only for sfSQLiteCache class.
<h4 class="sectionheader">sfMemcacheCache class</h4>
Defines host for the default Memcached server. This option is only available for sfMemcacheCache class. Default: localhost
Defines port for the default Memcached server. This option is only available for sfMemcacheCache class. Default: 11211
Defines whether the connection to Memcached must be persistent or not. This option is only available for sfMemcacheCache class. Possible values: <true | false> Default: true
Defines an array of additional servers (keys: host, port, persistent). See host, port and persistent options above. This option is only available for sfMemcacheCache class.
This factory is used to configure i18n core object. sfContext Accessor: $context→getI18N(). This factory is only defined if the i18n setting in the settings.yml file is set to true.
sfI18N class wraps the core i18n classes for a symfony context. Default: sfI18N
Defines the container type for translations. Possible values: <XLIFF | SQLite | MySQL | gettext>
Sets the debugging mode. If set to true, un-translated messages are decorated with a prefix and a suffix (see untranslated_prefix and untranslated_suffix options below). Possible values: <true | false> Default: false
Defines a prefix to used for un-translated messages. Default: «[T]»
Defines a suffix to used for un-translated messages. Default: «[/T]»
Defines a anonymous cache factory to be used for caching i18n data (see the view_cache factory for more information), eg.:
i18n: class: sfI18N param: ... cache: class: sfFileCache param: automatic_cleaning_factor: 0 cache_dir: %SF_I18N_CACHE_DIR% lifetime: 31556926 prefix: %SF_APP_DIR%/i18n
This factory is used to configure routing core object. sfContext Accessor: $context→getRouting().
sfPatternRouting class controls the generation and parsing of URLs. Default: sfPatternRouting
Specifies whether to enable logging. By default this option is equal to the one set in logging_enabled option in settings.yml file. Possible values: <true | false> Default: %SF_LOGGING_ENABLED%
Defines the list of characters that starts a variable name in a route pattern. Default: [:]
Defines the list of route segment separators. Most of the time, you don't want to override this option for the whole routing, but for specific routes. Default: [/, .]
Specifies whether to generate the shortest URL possible. If set to true, this option will tell the routing system to generate the shortest route possible by omitting empty route variables in the end of the URL. For example if you have this URL pattern:
/users/:username/:sort/:start/
the :username is set to 'test1' and the other two variables (:sort and :start) are left unset, but have default values defined, then the routing system will generate this URL:
/users/test1/
Set generate_shortest_url to false if you want your routes to be backward compatible with symfony 1.0 and 1.1. Possible values: <true | false> Default: true
When some parameters are not used in the generation of a route, this option allows those extra parameters to be converted to a query string. Set it to false to fallback to the behavior of symfony 1.0 or 1.1. In those versions, the extra parameters were just ignored by the routing system. Possible values: <true | false> Default: true
Defines an anonymous cache factory to be used for caching routing configuration and data (see the view_cache factory for more information), eg.:
routing: class: sfPatternRouting param: ... cache: class: sfFileCache param: automatic_cleaning_factor: 0 cache_dir: %SF_I18N_CACHE_DIR% lifetime: 31556926 prefix: %SF_APP_DIR%/i18n
The default suffix to use for all routes. This option is deprecated and is not useful anymore. Default: Empty string
Defines whether the routing.yml files must be automatically loaded and parsed. Set it to false if you want to use the routing system of symfony outside of a symfony project. Possible values: <true | false> Default: true
If set to true, this setting enables lazy unserialization of the routing cache. It can improve the performance of your applications if you have a large number of routes and if most matching routes are among the first ones. It is strongly advised to test the setting before deploying to production, as it can harm your performance in certain circumstances. Possible values: <true | false> Default: false
Determines how the routing cache is constructed. When set to false, the cache is stored as one big value; when set to true, each route has its own cache store. This setting is a performance optimization setting. As a rule of thumb, setting this to false is better when using a file-based cache class (sfFileCache for instance), and setting it to true is better when using a memory-based cache class (sfAPCCache for instance). Possible values: <true | false> Default: false
Defines regular expression used to adjust the characters allowed in your variable names. Default: [\w\d_]+
Specifies whether to enable caching or not. By default this option is equal to the one set in debug option in settings.yml file. Possible values: <true | false> Default: %SF_DEBUG%
Defines the default module name. Default: default
Defines the default action name. Default: index
This factory is used to configure logger core object. sfContext Accessor: $context→getLogger(). If you don't use the sfAggregateLogger, don't forget to specify a null value for the loggers parameter. This factory is always defined, but the logging only occurs if the logging_enabled setting in settings.yml file is set to true.
Defines PHP class used to create logger instance. Must extend sfLogger abstract class. Built-in logger classes:
Default: sfAggregateLogger
Defines the level of the logger. This parameter is available for all logger classes as it is used by sfLogger abstract class. Available levels:
Possible values: <emerg | alert | crit | err | warning | notice | info | debug>
<h4 class="sectionheader">sfWebDebugLogger class</h4>
Defines the web debug class. This option is only available for sfWebDebugLogger class. Default: sfWebDebug
<h4 class="sectionheader">sfFileLogger class</h4>
The file path or a php wrapper to log messages. You can use any supported php wrapper, eg. to write logs to the Apache error log, use php:stderr. This option is only available for sfFileLogger class. ====env_name > logger > param > format==== Defines the log line format. This option is only available for sfFileLogger class. Default: %time% %type% [%priority%] %message%%EOL% ====env_name > logger > param > type==== Defines the string which replaces %type% placeholder in the log line format. See format option above. This option is only available for sfFileLogger class. Default: symfony ====env_name > logger > param > time_format==== Defines the log time strftime format. This option is only available for sfFileLogger class. Default: %b %d %H:%M:%S ====env_name > logger > param > dir_mode==== Defines the mode to use when creating a directory. This option is only available for sfFileLogger class. Default: 0777 ====env_name > logger > param > file_mode==== Defines the mode to use when creating a file. This option is only available for sfFileLogger class. Default: 0666 <h4 class=«sectionheader»>sfVarLogger class</h4> ====env_name > logger > param > xdebug_logging==== Defines whether to add xdebug trace to the logs. This option is only available for sfVarLogger class. Possible values: <true | false> Default: false <h4 class=«sectionheader»>sfStreamLogger class</h4> ====env_name > logger > param > stream==== Defines a PHP stream. This option is only available for sfStreamLogger class. helper <h4 class=«sectionheader»>sfAggregateLogger class</h4> ====env_name > logger > param > loggers==== Defines an array of logger objects that extends sfLogger. This option is only available for sfAggregateLogger class. Eg.: <code yml> logger: class: sfAggregateLogger param: level: debug loggers: sf_web_debug: class: sfWebDebugLogger param: level: debug condition: %SF_WEB_DEBUG% xdebug_logging: false web_debug_class: sfWebDebug sf_file_debug: class: sfFileLogger param: level: debug file: %SF_LOG_DIR%/%SF_APP%_%SF_ENVIRONMENT%.log </code> =====Controller factory configuration===== ==== env_name > controller ==== This factory is used to configure controller core object. sfContext Accessor: $context→getController(). ==== env_name > controller > class ==== sfFrontWebController allows you to centralize your entry point in your web application, but at the same time allow for any module and action combination to be requested. This class does not take any options. Default: sfFrontWebController