gpt4 book ai didi

symfony - 在 symfony2 中加载应用程序的自定义配置文件

转载 作者:行者123 更新时间:2023-12-04 08:48:38 25 4
gpt4 key购买 nike

我正在开发一个 symfony2 应用程序,我正在尝试包含位于 /src/AppBundle/Resources/Config/general.yml 中的自定义 yaml 配置

我遵循此处提供的示例 http://symfony.com/doc/current/cookbook/bundles/extension.html并在 src/AppBundle/DependencyInjection/AppExtension.php 文件中创建,内容如下:

<?php
namespace AppBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Translation\Loader\YamlFileLoader;

class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator("@AppBundle/Resources/config')
);
$loader->load('general.yml');
}
}

然而,我卡在了这一点上,不知道如何让 symfony 执行这个文件并加载配置。

最佳答案

由于我没有看到您的 general.yml 文件的内容,我建议您使用类似下面的内容(我没有测试过,但应该没问题)。

假设这是你的 general.yml

doctrine:
orm:
entity_managers:
encrypt:
mappings:
MyEncryptBundle:
dir: Entity
type: annotations
prefix: My\EncryptBundle\Entity

无需创建此 yml 文件并导入它,您可以直接在 DependencyInjection 中设置所有内容,因此它会像下面这样。

namespace Application\FrontendBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Validator\Tests\Fixtures\Entity;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class ApplicationFrontendExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml'); # another file of yours
$loader->load('controllers.yml'); # another file of yours
$loader->load('repositories.yml'); # another file of yours
}

public function prepend(ContainerBuilder $container)
{
$container->prependExtensionConfig(
'doctrine',
[
'orm' => [
'entity_managers' => [
'encrypt' => [
'mappings' => [
'MyEncryptBundle' => [
'dir' => 'Entity',
'type' => 'annotation',
'prefix' => 'My\EncryptBundle\Entity'
]
]
]
]
]
]
);
}
}

或者你可以做点什么like this相反。

关于symfony - 在 symfony2 中加载应用程序的自定义配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31765136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com