Reputation: 782
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..
Upvotes: 65
Views: 115596
Reputation: 2184
Since Symfony 3.3,
You can use %kernel.project_dir%/web/
instead of %kernel.root_dir%/../web/
Upvotes: 0
Reputation: 1
$host = $request->server->get('HTTP_HOST');
$base = (!empty($request->server->get('BASE'))) ? $request->server->get('BASE') : '';
$getBaseUrl = $host.$base;
Upvotes: 0
Reputation: 1096
For Symfony3 In your controller try
$request->server->get('DOCUMENT_ROOT').$request->getBasePath()
Upvotes: 2
Reputation: 3070
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
Upvotes: 12
Reputation: 1016
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');
}
Upvotes: 44
Reputation: 22711
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');
Upvotes: 15
Reputation: 877
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).
Upvotes: 46
Reputation: 22386
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();
Upvotes: 18
Reputation: 169
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
Upvotes: 12