How to get the server path to the web directory in Symfony2 from inside the controller?
In Symfony2, you can use the $this->get('kernel')->getRootDir()
method inside a controller to get the server path to the web directory. The method returns the root directory of the project, which includes the web directory. You can then append the web directory to the path to get the full path to the web directory. For example:
$webDirectory = $this->get('kernel')->getRootDir() . '/web';
Watch a video course
Learn object oriented PHP
Alternatively, you can use the $this->getParameter('kernel.project_dir')
to get the path of project directory, which include the web
directory.
$webDirectory = $this->getParameter('kernel.project_dir') . '/web';
You can also use the $this->getParameter('kernel.public_dir')
to get the path of public directory, which is the web directory.
$webDirectory = $this->getParameter('kernel.public_dir');
Please note that the first method is deprecated in symfony 4.4 and removed in symfony 5.0.