Skip to content
Advertisement

getHost() or $_SERVER[‘SERVER_NAME’] from command component in Symfony2

I am executing a task (command component) in Symfony2 and I would like to use

$this->getRequest()->getHost()

just like I do in a controller. What is the way to get that value (/Command/MyCommand.php)?

I know I can do $_SERVER['SERVER_NAME']. But, I would like to use Symfony for this.

Advertisement

Answer

In case if you’d need host, you could get it from router context:

$host = $this->getContainer()->get('router')->getContext()->getHost();

But since you are looking for SERVER_NAME – it will not be available during script executing from console.

Workaround for this situation could be to define your parameter in parameters and get it Command:

$serverName = $this->getContainer()->getParameter('your_defined_param');
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement