问题
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