如何获取Symfony2应用程序的根目录? [英] How to get the root dir of the Symfony2 application?
问题描述
从控制器内部获取根应用程序目录的最佳方法是什么?可以在控制器外部获得它吗?
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%]
在Symfony2中是否有更好,更简单的方法来获取此信息?
Is there a better, simpler way to get this information in Symfony2?
推荐答案
更新2018-10-21:
从本周开始,getRootDir()
已被弃用.如Muzaraf Ali的评论部分所建议,请改用getProjectDir()
.
As of this week, getRootDir()
was deprecated. Please use getProjectDir()
instead, as suggested in the comment section by Muzaraf Ali.
-
使用此:
$this->get('kernel')->getRootDir();
如果需要网络根:
$this->get('kernel')->getRootDir() . '/../web' . $this->getRequest()->getBasePath();
这将通过控制器操作方法起作用...
this will work from controller action method...
至于服务,我认为您的做法尽可能简洁,尽管我会将完整的内核服务作为参数传递……但这也可以解决问题……
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...
这篇关于如何获取Symfony2应用程序的根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!