How to get the root dir of the Symfony2 application?

前端 未结 5 1250
耶瑟儿~
耶瑟儿~ 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:49

    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.

提交回复
热议问题