2
votes

I am start my first project in symfony 2. I use Symfony 2.6.6

My 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');
    }
}

And my 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');

    }
}

My configuration class src/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;
    }
}

And part of app/config/config.yml

karmed:
  var: off

I don't understand why i have this message when i called 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").  

I try remove all files from directory app/cache

I think the problem is configuration loading order, because when i try to change AppExtension to

.....
class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        print "exit";
        exit();
        $configuration = new Configuration();
.....

Nothing changes. But when i remove from config.yml my part of variables i can see my debug message.

1

1 Answers

7
votes

It's not that rootnode name that's used for the configuration, but the DI alias of the extension. As it's the AppExtension, it's alias is app. Configure it using app: instead.

However, AppBundle's aren't meant to be reused. It shouldn't use custom configuration things. Just use constants (best practice) or service parameters directly (recommended practice).