Reputation: 5016
I am trying to load a custom YAML config file within a bundle, which errors out, saying:
There is no extension able to load the configuration
The custom YAML config file:
#AppBundle/Resources/config/myconfig.yml
myapp: myvalue
The configuration file:
//AppBundle/DependencyInjection/MyConfiguration.php
namespace AppBundle\DependencyInjection;
use ...
class MyConfiguration implements ConfigurationInterface {
public function getConfigTreeBuilder() {
$treeBuilder = new TreeBuilder();
$treeBuilder->root('myapp')->end();
return $treeBuilder;
}
}
The extension file:
//AppBundle/DependencyInjection/AppExtension.php
namespace AppBundle\DependencyInjection;
use ...
class AppExtension extends Extension {
public function load(array $configs, ContainerBuilder $container)
{
$this->processConfiguration(new MyConfiguration(), $configs);
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('myconfig.yml');
}
}
The full error message:
InvalidArgumentException in YamlFileLoader.php line 399: There is no extension able to load the configuration for "myapp" (in C:\my_project\src\AppBundle\DependencyInjection/../Resources/config\myconfig.yml). Looked for namespace "myapp", found none
Upvotes: 1
Views: 887
Reputation: 6238
This is because you use the myapp
custom alias.
Symfony uses the underscored version of the bundle name by default (e.g. AcmeTestBundle
will be transformed to acme_test
).
Considering your bundle namespace, the app
alias will be the default.
You can change this code:
$treeBuilder->root('myapp')->end();
into this:
$treeBuilder->root('app')->end();
And then use the app
key in your configuration file.
This is the preferred solution, as it makes your configuration match your bundle names. If the configuration name doesn't suit you, maybe it's because the bundle is not named correctly!
You may have only one bundle in your app, which is called AppBundle
because you don't intend to reuse this bundle in any other project. If you still want to use a custom alias (myapp
instead of app
), here is the way to go:
<?php
//AppBundle/DependencyInjection/AppExtension.php
namespace AppBundle\DependencyInjection;
use ...
class AppExtension extends Extension {
public function load(array $configs, ContainerBuilder $container)
{
// ...
}
public function getAlias()
{
return 'myapp';
}
}
As Symfony will not like this alias by default, change your Bundle file as well:
<?php
// AppBundle/AppBundle.php
namespace AppBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
class AppBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function getContainerExtension()
{
if (null === $this->extension) {
$extension = $this->createContainerExtension();
if (null !== $extension) {
if (!$extension instanceof ExtensionInterface) {
throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
}
$this->extension = $extension;
} else {
$this->extension = false;
}
}
if ($this->extension) {
return $this->extension;
}
}
}
Upvotes: 1