Symfony捆绑包:如何避免使用__DIR__来构建主义映射的路径 [英] Symfony Bundle: how to avoid using __DIR__ to build the path to Doctrine mappings

查看:37
本文介绍了Symfony捆绑包:如何避免使用__DIR__来构建主义映射的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己已经在多个捆绑中处理相同的问题。

I'm finding myself dealing with the same problem in multiple bundles I've wrote.

问题在于我的 BundleNameBundle 类,我必须创建路径,然后加载Doctrine的映射。

The problem is that in my BundleNameBundle class I have to create the path to then load the mappings of Doctrine.

为此,我要做的事情是:

To do this I do something like:

/**
 * {@inheritdoc}
 */
public function build(ContainerBuilder $container)
{
    parent::build($container);

    $modelDir = realpath(__DIR__ . '/Resources/config/doctrine/mappings');
    $mappings = [
        $modelDir => 'SerendipityHQ\Bundle\QueuesBundle\Model',
    ];

    $ormCompilerClass = DoctrineOrmMappingsPass::class;
    if (class_exists($ormCompilerClass)) {
        $container->addCompilerPass(
            $this->getYamlMappingDriver($mappings)
        );
    }

    $container->addCompilerPass(new DaemonDependenciesPass());
}

完整代码此处

如您所见,我使用 __ DIR __ 获取映射所在文件夹的路径。

As you can see I use __DIR__ to get the path to the folder where the mappings are.

现在,Sensio Insights提醒我 = https://insight.sensiolabs.com/projects/2a1bcd2f-f241-4969-96e4-10ead299d57b/analyses/13#rule-22-014 rel = nofollow noreferrer>绝对路径常数 DIR FILE

Now, Sensio Insights is alerting me that "Absolute path constants DIR and FILE should not be used".

好,但是如何解决此问题?

Ok, but how can I solve this problem? Is there some alternative way to build the path to the mappings?

推荐答案

您可以使用 $ this- >路径。它返回与 __ DIR __

You can use $this->path. It returns the same result as __DIR__

这篇关于Symfony捆绑包:如何避免使用__DIR__来构建主义映射的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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