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

Accepted answer
Score: 47

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

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

Score: 44

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

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

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

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

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

  • If you are using apache as a webserver 4 (I suppose for other webservers the solution 3 would be similar) and are using virtualhosting 2 (your urls look like this - localhost/app.php then you can 1 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();
    
Score: 15

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

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

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

So 1 I can safely use this parameter within controller:

$this->getParameter('web_dir');
Score: 12

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

define('WEB_DIRECTORY', __DIR__);

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

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

Score: 12

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

You 4 can use the variable %assetic.write_to%.

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

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

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

Score: 2

For Symfony3 In your controller try

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

0

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

0

Score: 0

Since Symfony 3.3,

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

More Related questions