Symfony 4、从自定义类(不是控制器类)获取项目的根路径 [英] Symfony 4, get the root path of the project from a custom class (not a controller class)

查看:24
本文介绍了Symfony 4、从自定义类(不是控制器类)获取项目的根路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 src/Utils 目录中,我为各种事情创建了一个自定义类 Foo.我正在寻找一种方法来获取 symfony 4 项目的绝对根路径

In the src/Utils directory, I created a custom class Foo for various things. I'm looking for a way to get the absolute root path of the symfony 4 project

从控制器,它很容易:

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

但是从我在 src/Utils 目录中创建的自定义类,我怎样才能获得根路径目录?

But from a custom class I created in my src/Utils directory, how can I get the root path directory ?

我可以将路径从控制器传递给 Foo 类:

I could pass the path from the controller to the Foo class :

$webPath = $this->get('kernel')->getProjectDir();
$faa = new Foo($webPath);
$faa->doSomething();

但我认为将这些信息存储在 Foo 类中并在控制器类中只有控制器逻辑"更合适

but I think its more proper to store this information in the Foo class and have only "controller logic" in the controller class

推荐答案

在 Symfony AppKernel 类正在处理方法 getProjectDir() 下的项目根目录.要在控制器中获取它,您可以执行以下操作:

In Symfony AppKernel class is handling the project root directory under method getProjectDir(). To get it in the controller you can do:

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

它会返回一个项目根目录.

it will return you a project root directory.

如果您需要在您的课程之一中使用项目根目录,您有两种选择,我将提供给您.首先是将 AppKernel 作为依赖项传递:

If you need the project root directory in one of your classes you have two choices which I will present to you. First is passing AppKernel as dependency:

class Foo 
{
    /** KernelInterface $appKernel */
    private $appKernel;

    public function __construct(KernelInterface $appKernel)
    {
        $this->appKernel = $appKernel;
    }
}

感谢 Symfony 4 自动装配依赖项,它将自动注入到您的类中,您可以通过以下方式访问它:

Thanks to Symfony 4 autowiring dependencies it will be autmomaticaly injeted into your class and you could access it by doing:

$this->appKernel->getProjectDir();

但是请注意:我认为这不是一个好主意,除非您确实需要并且更多地使用 AppKernel 类而不是获取项目根目录.特别是如果您稍后考虑为您的班级创建单元测试.例如,您会因为需要创建 AppKernel 的模拟而自动增加复杂性.

But please notice: I don't think it's a good idea, until you have real need and more to do with AppKernel class than getting the project root dir. Specially if you think later on creating about unit tests for your class. You would automatically increase complexity by having a need to create mock of AppKernel for example.

第二个选项和恕我直言更好的是只传递一个带有目录路径的字符串.您可以通过在 config/services.yaml 中定义一个服务来实现这一点,如下所示:

Second option and IMHO better would be to pass only a string with path to directory. You could achieve this by defining a service inside config/services.yaml like this:

services:
    (...)
    MyNamespace\Foo:
        arguments:
            - %kernel.project_dir%

你的构造函数看起来像:

and your constructor would look like:

class Foo 
{
    /** string $rootPath */
    private $rootPath;

    public function __construct(string $rootPath)
    {
        $this->rootPath = $rootPath;
    }
}

这篇关于Symfony 4、从自定义类(不是控制器类)获取项目的根路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆