Symfony: get the working environment(Development or Production)

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

About deepak

Name:Deepak Kumar Country:India Education:B.Tech(Mechanical Engg.), MBA(Information Technology) Expertise:ISO, CMMI, ITIL, SQA, SQC, Web Development, Business & System Analysis, Business Process Analysis & Design, E-Business(B2B,B2C), Information Management & Strategy. Technologies:PHP, CSS, JAVA,AJAX, MySQL, Oracle etc. Interested In:Quality Assurance, Freelance Web Development, System Analysis & Friends
This entry was posted in Symfony, Technology and tagged , , , , . Bookmark the permalink.
blog comments powered by Disqus