交响乐2。没有能够加载配置的扩展。自定义配置块
Symfony2. There is no extension able to load the configuration. Custom configuration block
我在 symfony 2 中开始我的第一个项目。我使用 Symfony 2.6.6
我的AppKernel.php
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new FOS\UserBundle\FOSUserBundle(),
new AppBundle\AppBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
}
还有我的src/AppBundle/DependencyInjection\AppExtension.php
<?php
namespace AppBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.yml');
}
}
我的配置classsrc/AppBundle/DependencyInjection\Configuration.php
<?php
namespace AppBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree.
*
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('karmed');
$rootNode->children()->scalarNode('var')->end();
return $treeBuilder;
}
}
和app/config/config.yml
的一部分
karmed:
var: off
我不明白为什么我打电话给 app/console 时会收到这条消息?
[Symfony\Component\Config\Exception\FileLoaderLoadException] There is no extension able to load the configuration for "karmed" (in /home/vstas/data/work/karmed/app/config/config.yml). Looked for namespace "karmed", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "fos_user", "app", "debug", "web_profiler", "sensio_distribution" in /home/vstas/data/work/karmed/app/config/config.yml (which is being imported from "/home/vstas/data/work/karmed/app/config/config_dev.yml").
我尝试从目录 app/cache
中删除所有文件
我认为问题出在配置加载顺序上,因为当我尝试将 AppExtension 更改为
.....
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
print "exit";
exit();
$configuration = new Configuration();
.....
没有任何变化。
但是当我从 config.yml 中删除我的部分变量时,我可以看到我的调试消息。
用于配置的不是根节点名称,而是扩展的 DI 别名。因为它是 AppExtension,所以它的别名是 app
。使用 app:
配置它。
但是,AppBundle 并不意味着可以重复使用。它不应该使用自定义配置的东西。只需使用常量(最佳实践)或直接使用服务参数(推荐实践)。
我在 symfony 2 中开始我的第一个项目。我使用 Symfony 2.6.6
我的AppKernel.php
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new FOS\UserBundle\FOSUserBundle(),
new AppBundle\AppBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
}
还有我的src/AppBundle/DependencyInjection\AppExtension.php
<?php
namespace AppBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.yml');
}
}
我的配置classsrc/AppBundle/DependencyInjection\Configuration.php
<?php
namespace AppBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree.
*
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('karmed');
$rootNode->children()->scalarNode('var')->end();
return $treeBuilder;
}
}
和app/config/config.yml
的一部分karmed:
var: off
我不明白为什么我打电话给 app/console 时会收到这条消息?
[Symfony\Component\Config\Exception\FileLoaderLoadException] There is no extension able to load the configuration for "karmed" (in /home/vstas/data/work/karmed/app/config/config.yml). Looked for namespace "karmed", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "fos_user", "app", "debug", "web_profiler", "sensio_distribution" in /home/vstas/data/work/karmed/app/config/config.yml (which is being imported from "/home/vstas/data/work/karmed/app/config/config_dev.yml").
我尝试从目录 app/cache
中删除所有文件我认为问题出在配置加载顺序上,因为当我尝试将 AppExtension 更改为
.....
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
print "exit";
exit();
$configuration = new Configuration();
.....
没有任何变化。 但是当我从 config.yml 中删除我的部分变量时,我可以看到我的调试消息。
用于配置的不是根节点名称,而是扩展的 DI 别名。因为它是 AppExtension,所以它的别名是 app
。使用 app:
配置它。
但是,AppBundle 并不意味着可以重复使用。它不应该使用自定义配置的东西。只需使用常量(最佳实践)或直接使用服务参数(推荐实践)。