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

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..

This question is related to php symfony

The answer is


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

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


For Symfony3 In your controller try

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

Since Symfony 3.3,

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


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();
    

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');
}

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


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');