几个小时以来,我一直在努力做出你能想象到的最简单的事情而且它只是不起作用。我已经阅读了大量的stackoverflow问题,阅读有关配置文件的完整Symfony文档以及我阅读的每篇文章或其他信息,它变得越来越难以理解。
我创建了自己的Bundle。让我们称之为HappyBundle
。我把这个Bundle放在我公司的文件夹中。很自然地,我得到了CompanyHappyBundle
。
我想专门为这个bundle创建一个配置文件,因为我希望它可以重用。
我测试时创建了以下内容:
# src/Company/HappyBundle/Resources/config/config.yml
company_happy:
import:
path: /tmp
现在,我想要的是能够在我的Controller中使用此值。我只是不知道如何。它抛出了以下错误:
[Symfony\Component\Config\Exception\FileLoaderLoadException]
There is no extension able to load the configuration for "company_happy" (in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml).
Looked for namespace "company_happy", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution" in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml (which is being imported from "/home/user/symfony/app/config/config.yml").
在config.yml中,我添加了以下内容:
#app/config/config.yml
imports:
- { resource: "@CompanyHappyBundle/Resources/config/config.yml" }
我还制作了一个Configuration类,因为我读到了这个必需的地方。我真的认为这只是一个配置文件的工作。
namespace Company\HappyBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('company_happy');
$rootNode
->children()
->arrayNode('import')
->children()
->scalarNode('attachments_path')->defaultValue('/tmp')->end()
->scalarNode('method')->defaultValue('ALL')->end()
->booleanNode('move_mail')->defaultValue(true)->end()
->booleanNode('mark_read')->defaultValue(true)->end()
->end()
->end()
;
return $treeBuilder;
}
}
我实际需要的是实现这项工作所需的步骤和要求。 symfony的用处是有百万种方法可以做到这一点。文档并不仅仅提供工作流程。
有人可以帮帮我吗?
答案 0 :(得分:3)
一些通知:
在config.yml中,您尝试将import
定义为数组。似乎symfony不允许在配置的根目录中创建数组元素,这意味着您必须在树的下方嵌套数组。所以你做不到:
company_happy:
import:
path: /tmp
another_import:
...
我不确定这正是你想要做的,但你将import
定义为数组,这让我假设。
另一方面,你可以这样做:
company_happy:
imports:
import:
path: /tmp
another_import:
...
关于没有能够加载配置的扩展错误:确保您的扩展文件遵循命名对象。应该调用CompanyHappyExtension.php
并在其中定义CompanyHappyExtension
类。< / p>
我创建了一个示例CompanyHappyBundle包,它在Symofny 3上工作正常(也可能在S2上工作)。随意克隆/下载它:)
services.yml文件是一个额外的奖励,因为你无论如何都很需要它。
<强> SRC /公司/捆绑/ HappyBundle / CompanyHappyBundle.php:强>
<?php
namespace Company\Bundle\HappyBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class CompanyHappyBundle extends Bundle
{
}
<强> SRC /公司/捆绑/ HappyBundle / DependencyInjection / CompanyHappyExtension.php 强>
<?php
namespace Company\Bundle\HappyBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
class CompanyHappyExtension extends Extension implements ExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$configuration = new Configuration();
$options = $this->processConfiguration($configuration, $configs);
// Do something with your options here
}
}
<强> SRC /公司/捆绑/ HappyBundle / DependencyInjection /的configuration.php 强>
<?
namespace Company\Bundle\HappyBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('company_happy');
$rootNode
->children()
->arrayNode('imports')
->prototype('array')
->children()
->scalarNode('path')->defaultValue('/tmp')->end()
->scalarNode('method')->defaultValue('ALL')->end()
->booleanNode('move_mail')->defaultValue(true)->end()
->booleanNode('mark_read')->defaultValue(true)->end()
->end()
->end()
->end()
;
return $treeBuilder;
}
}
<强> SRC /公司/捆绑/ HappyBundle /资源/配置/ config.yml 强>
company_happy:
imports:
import:
path: /tmp
<强> SRC /公司/捆绑/ HappyBundle /资源/配置/ services.yml 强>
# Define your services here
services: