2

我有一个带有 MicroKernel 的应用程序,并且想使用带有自定义配置的 Symfony 的 TreeBuilder,因为该应用程序将非常通用,我希望尽可能多地在外部进行配置。我的问题是,我无法namespace为 symfony 注册我的自定义:

There is no extension able to load the configuration for "research" 
(in /var/www/html/src/app/config/config.yml). Looked for 
namespace "research", found "framework", "twig", 
"sensio_framework_extra", "web_profiler"

我有这Configuration堂课:

<?php
namespace App\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('research');

        // @formatter:off
        $rootNode
            ->children()
                ->arrayNode('questions')
                    ->children()
                        ->integerNode('client_id')->end()
                        ->scalarNode('client_secret')->end()
                    ->end()
                ->end()// twitter
            ->end();
        // @formatter:on

        return $treeBuilder;
    }
}

我的AppExtension

<?php
namespace App\DependencyInjection;

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

class AppExtension extends ConfigurableExtension
{
    // note that this method is called loadInternal and not load
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
    {
        dump($mergedConfig);
    }

    public function getAlias()
    {
        return 'research';
    }
}

在我的AppKernel课堂上,我正在像这样初始化扩展,因此我还从我原来的调用中获得了输出dump($mergedConfig)AppExtension research.yml

<?php
namespace App;

use App\DependencyInjection\AppExtension;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

$loader = require __DIR__ . '/../vendor/autoload.php';

// auto-load annotations
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

/**
 * Class AppKernel
 */
class AppKernel extends Kernel
{
    use MicroKernelTrait;

    ...

    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
    {
        $researchConfig = Yaml::parse(file_get_contents(__DIR__ . '/config/research.yml'));
        $appExtension = new AppExtension();
        $appExtension->load(researchConfig, $c);

        $loader->load(__DIR__ . '/config/config.yml');
    }

    ...
}

然而,当我config.yml看起来像这样时,我得到了上面提到的错误:

framework:
    secret: S0ME_SUPER_SECRET
    profiler: { only_exceptions: false }


research:
    questions:
       client_id: 2342323423
       clent_secret: mysecret

那么我需要做什么来实际注册我的命名空间研究,以便我可以正确覆盖它?还是需要外部供应商捆绑包?

4

2 回答 2

3

你得到错误是因为你没有注册一个包,所以 symfony 不知道包配置名称空间。

我不能说你如何用你的方法解决这个问题,但我建议制作一个 AppBundle 而不是 App 并在public function registerBundles().

请参阅一些样板中的 fE,例如https://github.com/ikoene/symfony-micro

或者,您可以尝试使用新的 symfony Flex 设置您的项目。
这已经使用了 MicroKernel,是无捆绑的,并且还支持最新的稳定 symfony 版本(sf3.3)。
https://symfony.com/doc/current/setup/flex.html#using-symfony-flex-in-new-applications

因此,无需自己在赛道外建立一些东西。

于 2017-11-15T13:34:33.930 回答
0

由于 Symfony 不再推荐使用 Bundles (),创建一个 Bundle不是解决这个问题的方法。

@see [ https://symfony.com/doc/current/bundles.html][1]

在 Symfony 4.0 之前的版本中,建议使用包来组织您自己的应用程序代码。不再建议这样做,并且捆绑包只能用于在多个应用程序之间共享代码和功能。

[1]:Symfony 包文档

于 2020-02-13T13:49:19.530 回答