首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为Symfony MicroKernel注册自定义配置命名空间

为Symfony MicroKernel注册自定义配置命名空间
EN

Stack Overflow用户
提问于 2017-11-15 10:47:27
回答 2查看 654关注 0票数 2

我有一个带有MicroKernel的应用程序,我想使用Symfony的TreeBuilder和自定义配置,因为这个应用程序将是相当通用的,我希望尽可能多地在外部配置。我的问题是,我无法为symfony注册我的自定义namespace

代码语言:javascript
复制
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类:

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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';
    }
}

在我的dump($mergedConfig)类中,我像这样初始化扩展,因此,AppExtension中的调用也从原来的research.yml中得到了一个输出

代码语言:javascript
复制
<?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看起来像这样时,我会得到上面提到的错误:

代码语言:javascript
复制
framework:
    secret: S0ME_SUPER_SECRET
    profiler: { only_exceptions: false }


research:
    questions:
       client_id: 2342323423
       clent_secret: mysecret

那么,我需要做什么,才能真正注册我的命名空间研究,这样我才能正确地覆盖它?或者它必须要一个外部供应商包?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-15 13:34:33

您会得到错误,因为您没有注册包,因此symfony不知道bundle配置命名空间。

我不能告诉你如何用你的方法来解决这个问题,但我建议用AppBundle代替应用程序,并在public function registerBundles()中注册AppBundle。

见f.E.中的一些样板,如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

所以你不需要自己做一些偏离轨道的事情。

票数 3
EN

Stack Overflow用户

发布于 2020-02-13 13:49:19

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

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

在4.0之前的Symfony版本中,建议使用包来组织自己的应用程序代码。这不再是推荐的,而包应该只用于在多个应用程序之间共享代码和特性。

1: Symfony Bundle文档

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47305390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档