且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从控制器内部获取到 Symfony2 中 web 目录的服务器路径?

更新时间:2023-11-19 21:46:22

实际上没有直接的方法可以在 Symfony2 中获得 webdir 的路径,因为框架完全独立于 webdir.

There's actually no direct way to get path to webdir in Symfony2 as the framework is completely independent of the webdir.

您可以在内核类的实例上使用 getRootDir(),就像您编写的那样.如果您考虑在将来重命名 /web 目录,您应该使其可配置.例如 AsseticBundle 在其 DI 配置中有这样一个选项(参见这里此处).

You can use getRootDir() on instance of kernel class, just as you write. If you consider renaming /web dir in future, you should make it configurable. For example AsseticBundle has such an option in its DI configuration (see here and here).