Is there an easy way to get the ServerName in Symfony?
Solution 1
I think I found something :
$this->getRequest()->getHost()
This seems to work... It will work only if there is a request of course, so it is not universal. So, this won't work using CLI.
Solution 2
Not very nice, but I use $_SERVER["SERVER_NAME"]
in my front controller file, and use that to determine the environment I'm activating:
$env = "prod";
if (preg_match("/qahost\.tld$/", $_SERVER["SERVER_NAME"]))
{
$env = "qa";
}
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', $env, false);
sfContext::createInstance($configuration)->dispatch();
I'd be interested to see the proper approach :-)
Related videos on Youtube
greg0ire
favorite tools : git, zsh, sass favorite frameworks: Symfony* favorite languages: php, python
Updated on June 25, 2021Comments
-
greg0ire almost 3 years
I have been searching for an easy way to get the ServerName of the machine where Symfony runs in Symfony (so that my app adapts when it is used on a host with a different ServerName), but I couldn't find one. I created a variable in app.yml and I fetch it, but I still wonder if there is no easier way to do this. How are you doing this? I'm using Symfony 1.2 and 1.4 on different projects.
-
mamoo almost 14 yearsCan't you use gethostname()? php.net/manual/en/function.gethostname.php
-
greg0ire almost 14 yearsSorry, I do not want the hostname in fact, but the ServerName (I edited my post)
-
-
greg0ire almost 14 yearsA colleague just told me that we should not rely on this, because there are many risks. He thinks the setting in YML file is the best solution...
-
Flukey almost 14 yearscould you elaborate on the risks please? :-)
-
greg0ire almost 14 yearsThere are at leas two situations where you do not want to rely on such a system : - when you do not use apache (when sending mails via a cronjob for instance) - when you have several servers but you want to promote your site buy using only one of them.