Search code examples
symfonysymfony-2.7

Symfony 2.7 retrieve root directory ANYWHERE


I just want to ask if there's a way to retrieve the root directory of a Symfony Application ANYHWERE?

What I mean by anywhere is, in any file of my App.

I've searched everywhere and all I get is this:

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

Which of course works! But I can't use it in my custom classes. I need to get the root directory in one of my custom classes.

I've already read answers about DependencyInjection/Service and other stuff, but I think it's too complex/overkill to implement those just to solve my current problem.

I just want the root directory of my app, period. Is there any other way?


Solution

  • The simplest way I can think of is to define a constant in your app.php file, like this:

    define("ROOTDIR", $kernel->getRootDir());
    

    so you can then use this constant anywhere. Compared to this, a static method is overkill, too.