How to get the server path to the web directory in Symfony2 from inside the controller?

PhpSymfony

Php Problem Overview


The question is as follows:

How can I get the server path to the web directory in Symfony2 from inside the controller (or from anywhere else for that reason)

What I've already found (also, by searching here):

This is advised in the cookbook article on Doctrine file handling

$path = __DIR__ . '/../../../../web';

Found by searching around, only usable from inside the controller (or service with kernel injected):

$path = $this->get('kernel')->getRootDir() . '/../web';

So, is there absolutely no way to get at least that 'web' part of the path? What if I, for example, decided to rename it or move or something?

Everything was easy in the first symfony, when I could get like everything I needed from anywhere in the code by calling the static sfConfig::get() method..

Php Solutions


Solution 1 - Php

There's actually no direct way to get path to webdir in Symfony2 as the framework is completely independent of the webdir.

You can use getRootDir() on instance of kernel class, just as you write. If you consider renaming /web dir in future, you should make it configurable. For example AsseticBundle has such an option in its DI configuration (see here and here).

Solution 2 - Php

To access the root directory from outside the controller you can simply inject %kernel.root_dir% as an argument in your services configuration.

service_name:
    class: Namespace\Bundle\etc
    arguments: ['%kernel.root_dir%']

Then you can get the web root in the class constructor:

public function __construct($rootDir)
{
    $this->webRoot = realpath($rootDir . '/../web');
}

Solution 3 - Php

You also can get it from any ContainerAware (f.i. Controller) class from the request service:

  • If you are using apache as a webserver (I suppose for other webservers the solution would be similar) and are using virtualhosting (your urls look like this - localhost/app.php then you can use:

     $container->get('request')->server->get('DOCUMENT_ROOT');
     // in controller:
     $this->getRequest()->server->get('DOCUMENT_ROOT');
    
  • Else (your urls look like this - localhost/path/to/Symfony/web/app.php:

     $container->get('request')->getBasePath();
     // in controller:
     $this->getRequest()->getBasePath();
    

Solution 4 - Php

You are on Symfony, think "Dependency Injection" ^^

In all my SF project, I do in parameters.yml:

web_dir: "%kernel.root_dir%/../web"

So I can safely use this parameter within controller:

$this->getParameter('web_dir');

Solution 5 - Php

My solution is to add this code to the app.php

define('WEB_DIRECTORY', __DIR__);

The problem is that in command line code that uses the constant will break. You can also add the constant to app/console file and the other environment front controllers

Another solution may be add an static method at AppKernel that returns DIR.'/../web/' So you can access everywhere

Solution 6 - Php

UPDATE: Since 2.8 this no longer works because assetic is no longer included by default. Although if you're using assetic this will work.

You can use the variable %assetic.write_to%.

$this->getParameter('assetic.write_to');

Since your assets depend on this variable to be dumped to your web directory, it's safe to assume and use to locate your web folder.

http://symfony.com/doc/current/reference/configuration/assetic.html

Solution 7 - Php

For Symfony3 In your controller try

$request->server->get('DOCUMENT_ROOT').$request->getBasePath()

Solution 8 - Php

$host = $request->server->get('HTTP_HOST');
$base = (!empty($request->server->get('BASE'))) ? $request->server->get('BASE') : '';
$getBaseUrl = $host.$base;

Solution 9 - Php

Since Symfony 3.3,

You can use %kernel.project_dir%/web/ instead of %kernel.root_dir%/../web/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSylvixView Question on Stackoverflow
Solution 1 - PhpMartinView Answer on Stackoverflow
Solution 2 - Phpredjam13View Answer on Stackoverflow
Solution 3 - PhpMolecular ManView Answer on Stackoverflow
Solution 4 - PhpThomas DecauxView Answer on Stackoverflow
Solution 5 - PhpnoelView Answer on Stackoverflow
Solution 6 - PhpTekView Answer on Stackoverflow
Solution 7 - Phputkarsh2k2View Answer on Stackoverflow
Solution 8 - PhpAshokView Answer on Stackoverflow
Solution 9 - PhpTsounabeView Answer on Stackoverflow