Php – Symfony2 get root directory path from command class

PHPsymfony

What is the way to get root directory path from command class in symfony 2 ? Note : Not from controller class as I am creating a CLI command.

Or is there a a way to define a path in config file and access in anywhere ?

Best Answer

By extending ContainerAwareCommand you can access the root directory path using:

$this->getContainer()->get('kernel')->getRootDir()

See http://symfony.com/doc/current/cookbook/console/console_command.html#getting-services-from-the-service-container