php 如何获取 Symfony2 应用程序的根目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9215753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:22:39  来源:igfitidea点击:

How to get the root dir of the Symfony2 application?

phpsymfonysymfony-2.8symfony-config-component

提问by Dawid Ohia

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%]

Is there a better, simpler way to get this information in Symfony2?

在 Symfony2 中是否有更好、更简单的方法来获取这些信息?

回答by Jovan Perovic

UPDATE 2018-10-21:

更新 2018-10-21:

As of this week, getRootDir()was deprecated. Please use getProjectDir()instead, as suggested in the comment section by Muzaraf Ali.

截至本周,getRootDir()已弃用。请getProjectDir()改用,正如穆扎拉夫·阿里 (Muzaraf Ali) 在评论部分中所建议的那样。

—-

----

Use this:

用这个:

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

And if you want the webroot:

如果你想要网络根目录:

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

编辑:至于服务,我认为你做的方式尽可能干净,虽然我会通过完整的内核服务作为参数......但这也能解决问题......

回答by Chris

In Symfony 3.3you can use

在 Symfony 3.3 中你可以使用

$projectRoot = $this->get('kernel')->getProjectDir();

to get the web/project root.

获取网络/项目根目录。

回答by MauganRa

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:

如果您使用此路径访问不是代码的项目部分(例如上传目录或 SQLite 数据库),那么最好将路径转换为参数,如下所示:

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.

更新:修复了 kernel.root_dir 常量用法。

回答by Tragaknight

You can also use regular expression in addition to this:

除此之外,您还可以使用正则表达式:

    $directoryPath = $this->container->getParameter('kernel.root_dir') . '/../web/bundles/yourbundle/';
    $directoryPath = preg_replace("/app..../i", "", $directoryPath);
    echo $directoryPath;

回答by Andrew Zhilin

Since Symfony 3.3 you can use binding, like

从 Symfony 3.3 开始,你可以使用绑定,比如

services:
_defaults:
    autowire: true      
    autoconfigure: true
    bind:
        $kernelProjectDir: '%kernel.project_dir%'

After that you can use parameter $kernelProjectDir in any controller OR service. Just like

之后,您可以在任何控制器或服务中使用参数 $kernelProjectDir。就像

class SomeControllerOrService
{
    public function someAction(...., $kernelProjectDir)
    {
          .....