Monday, February 13, 2012

Symfony: get the working environment(Development or Production) | DecentMind

Symfony: get the working environment(Development or Production) | DecentMind

While developing an application in Symfony , You might want to run some part of your code in development environment and other part in production environment. In such situation you can use sfConfig::get class to get the environment in which your application is running, And you can use this as a condition to run your code.
web/index.php is the file where you declare your Symfony environment:

define('SF_ROOT_DIR',realpath(dirname(__FILE__).'/..')); define('SF_APP','myapp'); define('SF_ENVIRONMENT', 'prod'); define('SF_DEBUG', false); require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'); sfContext::getInstance()->getController()->dispatch();

Now you can get the environment using following:

$sf_environment = sfConfig::get('sf_environment');

$sf_environment variable will return string as ‘prod’ and you can use it as condition

$sf_environment = sfConfig::get('sf_environment'); if($sf_environment==prod) {     do this..... } else {     do this..... }

In-fact you can use sfConfig::get to access any configuration of your application declared in yml files.

Happy Coding….. :)



More than 3 requests, I'll translate this to Chinese.
超过3个请求,我就会把这篇文章翻译成中文。

1 comment:

ALGO said...

To get all available options: getAll()

To get current application: get('sf_app')