Symfony AppExtension not loaded

耗尽温柔 提交于 2019-12-12 19:13:42

问题


I am using Symfony 4 with the preconfigured "App"-Bundle (it does not have "Bundle" in its name) and added an extension as below. When accessing routes, the debug output from the extension is not outputted so the extension logic is not running. Why? I did not register the extension anywhere, just followed the manual at http://symfony.com/doc/current/bundles/extension.html.

<?php
namespace App\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AppExtension extends Extension
{
    public function __construct()
    {
        echo "EXTLOAD000|";
    }

    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        echo "EXTLOAD|";
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        $container->setParameter('app_transformer_configuration', $config['transformer_configuration']);
    }
}

The code except for the debug is from this site: https://blog.bam.tech/developper-news/make-a-versioned-api-with-symfony (Symfony 2.7)


回答1:


Update: I was doing this the hard way. Extensions can be loaded using Kernel::build()

// src/Kernel.php
protected function build(ContainerBuilder $container): void
{
    $container->registerExtension(new AppExtension());
}

The rest of this can be ignored now though I'm keeping it here just because I spent the time to type it in.

The way to register your AppExtension is to create an AppBundle. Which is just a simple class.

// src/AppBundle.php
namespace App;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle
{

}

Register it:

// config/bundles.php
return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true],
    App\AppBundle::class => ['all' => true],
];

And then your extension will be called:

// src/DependencyInjection/AppExtension.php
namespace App\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        die('load');
    }
}

I think this is what you are asking for. Still not really clear why you would need to do this for a S4 app but that is okay.




回答2:


Try dump function, like that

public function __construct()
{
    dump('Extension loaded successfully!');exit;
}


来源:https://stackoverflow.com/questions/49780704/symfony-appextension-not-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!