Symfony AppExtension 未加载 [英] Symfony AppExtension not loaded

查看:28
本文介绍了Symfony AppExtension 未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Symfony 4 与预配置的App"-Bundle(名称中没有Bundle")一起使用,并添加了如下扩展名.访问路由时,不会输出扩展的调试输出,因此扩展逻辑未运行.为什么?我没有在任何地方注册扩展程序,只是按照 http://symfony.com 上的手册进行操作/doc/current/bundles/extension.html.

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

除debug之外的代码来自本站:https://blog.bam.tech/developper-news/make-a-versioned-api-with-symfony (Symfony 2.7)

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)

推荐答案

更新:我在这方面做得很艰难.可以使用 Kernel::build()

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.

注册您的 AppExtension 的方法是创建一个 AppBundle.这只是一个简单的类.

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
{

}

注册:

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

我认为这就是您要的.仍然不清楚为什么您需要为 S4 应用程序执行此操作,但这没关系.

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.

这篇关于Symfony AppExtension 未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆