我正在开发一个symfony2
应用程序,我试图包含我的自定义 yaml 配置位于/src/AppBundle/Resources/Config/general.yml
我已按照此处提供的示例进行操作http://symfony.com/doc/current/cookbook/bundles/extension.html 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
您可以直接在 DependencyInjection 中设置所有内容,而不是创建此 yml 文件并导入它,如下所示。
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'
]
]
]
]
]
]
);
}
}
或者你可以做点什么像这样 http://www.inanzzz.com/index.php/post/aqkg/how-to-add-a-custom-configuration-parameters-to-config-yml-file-and-accessing-them-in-symfony-application反而。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)