How to get the root dir of the Symfony2 application?

前端 未结 5 1355
耶瑟儿~
耶瑟儿~ 2021-01-29 23:36

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 param

5条回答
  •  感动是毒
    2021-01-29 23:42

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

提交回复
热议问题