1
votes

Following the advice posted at Loading custom config file for app in symfony2 I stucked into namespace issue.

In \src\AppBundle\DependencyInjection I have two files:

AppExtension.php:

<?php
namespace AppBundle\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;
use AppBundle\DependencyInjection\Configuration;

/**
 * This is the class that loads and manages your bundle configuration
 */
class AppExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $container->setParameter('kh', $config['kh']);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('general.yml'); # another file of yours

    }
}

and Configuration.php:

<?
namespace AppBundle\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('kh');

         $rootNode
            ->children()
            ->arrayNode('tags')
            ->prototype('array')
                ->children()
                    ->scalarNode('name')->isRequired()->end();
                    ->scalarNode('role')->isRequired()->end();
                    ->scalarNode('priority')->isRequired()->end();
                    ->scalarNode('label')->isRequired()->end();
                ->end();
            ->end();
        ->end();

        return $treeBuilder;
    }
}

It seems that I make some kind of mistake naming the files as executing:

php app/console cache:clear --env=prod

results in:

PHP Fatal error: Class 'AppBundle\DependencyInjection\Configuration' not found in /var/www/dev.investmentopportunities.pl/src/AppBundle/DependencyInjection/AppExtension.php on line 22 [2015-08-02 10:39:36] php.EMERGENCY: Fatal Error: Class 'AppBundle\DependencyInjection\Configuration' not found {"type":1,"file":"/var/www/dev.investmentopportunities.pl/src/AppBundle/DependencyInjection/AppExtension.php","line":22,"level":6143,"stack":[]}

Unluckily I have a problem with finding the error on my own. I have tried to rename files but still the error occurs. Could you please advice what am I doing wrong?

UPDATE 1:

As sugested by @San Thapa I have tried also to remove "use" command. This results in:

PHP Fatal error: Class 'AppBundle\DependencyInjection\Configuration' not found in /var/www/dev.investmentopportunities.pl/src/AppBundle/DependencyInjection/AppExtension.php on line 21 [2015-08-02 12:05:27] php.EMERGENCY: Fatal Error: Class 'AppBundle\DependencyInjection\Configuration' not found {"type":1,"file":"/var/www/dev.investmentopportunities.pl/src/AppBundle/DependencyInjection/AppExtension.php","line":21,"level":6143,"stack":[]}

[Symfony\Component\Debug\Exception\ClassNotFoundException] Attempted to load class "Configuration" from namespace "AppBundle\Dependenc yInjection". Did you forget a "use" statement for e.g. "Symfony\Bundle\TwigBundle\Depend encyInjection\Configuration", "Symfony\Bundle\DebugBundle\DependencyInjecti on\Configuration", "Symfony\Bundle\WebProfilerBundle\DependencyInjection\Co nfiguration", "Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestB undle\DependencyInjection\Configuration", "Symfony\Bundle\FrameworkBundle\D ependencyInjection\Configuration", "Genemu\Bundle\FormBundle\DependencyInje ction\Configuration", "Doctrine\ORM\Configuration", "Doctrine\DBAL\Configur ation", "Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\Configurat ion", "Symfony\Bundle\SwiftmailerBundle\DependencyInjection\Configuration", "Symfony\Bundle\MonologBundle\DependencyInjection\Configuration", "Symfony \Bundle\AsseticBundle\DependencyInjection\Configuration", "Sensio\Bundle\Fr ameworkExtraBundle\DependencyInjection\Configuration", "PUGX\AutocompleterB undle\DependencyInjection\Configuration", "Knp\Bundle\PaginatorBundle\Depen dencyInjection\Configuration", "FOS\UserBundle\DependencyInjection\Configur ation" or "Doctrine\Bundle\DoctrineBundle\DependencyInjection\Configuration "?

What seems a bit strange acessing the webpage from browser does not result in any error.

1
No need to use the namespace in this case as both the files resides on same directory and enclosed by same namespace, you can use it directly without use statement.San Thapa
Hi thanks for clue. I tried it at beginning but still it caused error.Abdel5
If you are running the project on development environment try clearing cache on development too. Clearing cache has worked for me most of the times.San Thapa

1 Answers

1
votes

Just added to first line of Configuration.php:

<?php

and also remove semicolons:

$rootNode
        ->children()
        ->arrayNode('tags')
        ->prototype('array')
            ->children()
                ->scalarNode('name')->isRequired()->end()
                ->scalarNode('role')->isRequired()->end()
                ->scalarNode('priority')->isRequired()->end()
                ->scalarNode('label')->isRequired()->end()
            ->end()
        ->end()
    ->end();

I'm not sure about structure... Maybe you forgot add one more end()...