We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Am I on product or localhost? How to detect the environment?

how to determine that the site is running on production or developer environment?



9.8k
edited Sep '15

I'm not sure what you mean. I think there are many ways of determining or checking if you are on a production or development environment. I would do this way I would check what $_SERVER['SERVER_NAME'] returns and if i decided that localhost is development environment and $_SERVER... returns 'localhost' is development f.e. localhost i could set the standard $config variable value into $config->development=true (after config is initialised or during initialisation with ("development" => ($_SERVER['SERVER_NAME'] =="localhost")?true:false) or something like this ) This in turn (i assume you register the config in the di variable as Phalcon tutorials show) could be used in your controller action this way (if you wanted) $this->config->development or you could use differnt settings for database for di. Some other users could give you some better tips.



28.1k

for example, I want to use different connections for local developing, and for products. So I have to distinguish the two (and I dont want to always edit the settings when uploading)



9.8k
edited Sep '15

If you set the config variable as i suggested (you don't need to but you then have the config variable accessible in controller, models, etc) you could do something like this


$di->set('db', function () use ($config) {

    if ($config->development===true) {

      if (strtolower($config->database->adapter)=="mysql") {

          return new DbAdapter(array(
              'adapter'     => $config->databasedev->adapter,
              'host' => $config->databasedev->host,
              'username' => $config->databasedev->username,
              'password' => $config->databasedev->password,
              'dbname' => $config->databasedev->dbname
          ));

      }    
    } else {
      if (strtolower($config->database->adapter)=="mysql") {

          return new DbAdapter(array(
              'adapter'     => $config->database->adapter,
              'host' => $config->database->host,
              'username' => $config->database->username,
              'password' => $config->database->password,
              'dbname' => $config->database->dbname
          ));

      }    
    }  
});

Your other services (like autoloader autoloading directories/classess, namespaces) could be easily switched in a similar way.

edited Sep '15

You could use the example by @OpenEX but nstead of settings a development property of config, do this:

$di->set('db', function () use ($config) use ($di) {
    if ($di->get('request')->getHttpHost()=='your.dev.host') {
        // dev
    } else {
        // prod
    }
}