How to get the root dir of the Symfony2 application?
Asked Answered
A

5

126

What is the best way to get the root app directory from inside the controller? Is it possible to get it outside of the controller?

Now I get it by passing it (from parameters) to the service as an argument, like this:

services:

    sr_processor:
        class: Pro\Processor
        arguments: [%kernel.root_dir%]

Is there a better, simpler way to get this information in Symfony2?

Aryan answered 9/2, 2012 at 17:29 Comment(4)
your solution is bestClementeclementi
youre mixing up a service and a controller yoUvea
Again for Symfony 3.3 and onwards use %kernel.project_dir% to get to root of your project.Knapweed
be sure to encapsulate literals in quotes: ["%kernel.root_dir%"]Forewoman
P
221

UPDATE 2018-10-21:

As of this week, getRootDir() was deprecated. Please use getProjectDir() instead, as suggested in the comment section by Muzaraf Ali.

—-

Use this:

$this->get('kernel')->getRootDir();

And if you want the web root:

$this->get('kernel')->getRootDir() . '/../web' . $this->getRequest()->getBasePath();

this will work from controller action method...

EDIT: As for the services, I think the way you did it is as clean as possible, although I would pass complete kernel service as an argument... but this will also do the trick...

Perrault answered 9/2, 2012 at 17:34 Comment(10)
+1 but not accurate for symfony2. Use instead: $this->get('kernel')->getRootDir() . '/../..' . $this->getRequest()->getBasePath();Vhf
Are you sure? I have successfully used this code in 2.0 -> 2.2. Was directory structure changed in 2.3 or master?Perrault
This solution is only right for controllers. Here the issue is about services. Anyway, why passing the whole container just to get a variable? It's far better to pass just %kernel.root_dir%Pansophy
Massimiliano is right, and as an addition, i use $this->container->getParameter('kernel.cache_dir') in controllers instead of the solution you providedMadelinemadella
I agree, while this solution was acceptable in that time I feel like there are way more better solutions presently...Perrault
@JovanPerovic ...such as... ?Citation
This was more on topic of your service having container injected :) If you follow best practices, injecting the container should be avoided at all costs. As for the solution of getting web directory, I still use the one I provided above ;)Perrault
why not just using __DIR__ thenDuct
the __DIR__ represents the current directory of class, so that would not work...Perrault
As of Symfony 3.3 and onwards use %kernel.project_dir%/web/ instead of %kernel.root_dir%/../web/Knapweed
H
16

In Symfony 3.3 you can use

$projectRoot = $this->get('kernel')->getProjectDir();

to get the web/project root.

Hyder answered 21/9, 2017 at 11:37 Comment(0)
P
8

If you are using this path to access parts of the projects which are not code (for example an upload directory, or a SQLite database) then it might be better to turn the path into a parameter, like this:

parameters:
    database_path: '%kernel.root_dir%/../var/sqlite3.db'

This parameter can be injected everywhere you need it, so you don't have to mess around with paths in your code any more. Also, the parameter can be overridden at deployment time. Finally, every maintaining programmer will have a better idea what you are using it for.

Update: Fixed kernel.root_dir constant usage.

Paganini answered 25/8, 2016 at 18:18 Comment(0)
A
7

You can also use regular expression in addition to this:

    $directoryPath = $this->container->getParameter('kernel.root_dir') . '/../web/bundles/yourbundle/';
    $directoryPath = preg_replace("/app..../i", "", $directoryPath);
    echo $directoryPath;
Autonomous answered 20/1, 2015 at 16:18 Comment(0)
N
3

Since Symfony 3.3 you can use binding, like

services:
_defaults:
    autowire: true      
    autoconfigure: true
    bind:
        $kernelProjectDir: '%kernel.project_dir%'

After that you can use parameter $kernelProjectDir in any controller OR service. Just like

class SomeControllerOrService
{
    public function someAction(...., $kernelProjectDir)
    {
          .....
Nerves answered 20/9, 2019 at 9:44 Comment(1)
This answer deserves more upvotes. This is the best practice as of Symfony 3.3.Insusceptible

© 2022 - 2024 — McMap. All rights reserved.