2017-12-21 52 views
3

여기에 제공된 설명서에 따라 내 응용 프로그램에서 yaml 구성 파일을 가져 오려고합니다 : http://symfony.com/doc/current/bundles/extension.html하지만 항상 오류 메시지가 나타납니다 : 확장 기능이 없습니다 "응용 프로그램"Symfony 4 DI 확장 클래스의 사용자 Yaml 설정 파일로드 및 처리

내 파일은 다음 위치에 대한 구성을로드합니다 :/애플리케이션 제목을 다음과 같은 구조를 가지고 설정/패키지 :이로

app: 
    list: 
     model1: 
      prop1: value1 
      prop2: value2 
     model2: 
      ... 

간단한 응용 프로그램입니다, 모든 파일은 "src /"에 있습니다. 그래서 나는이 :
SRC/의존성 주입/AppExtension.php

<?php 

namespace App\DependencyInjection; 

use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\Config\FileLocator; 
use Symfony\Component\HttpKernel\DependencyInjection\Extension; 
use Symfony\Component\DependencyInjection\Loader; 

class AppExtension extends Extension 
{ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     $configuration = new Configuration(); 
     $config = $this->processConfiguration($configuration, $configs); 
    } 
} 

SRC/의존성 주입 /을 configuration.php

<?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('app'); 

     // Node definition 
     $rootNode 
      ->children() 
       ->arrayNode('list') 
        ->useAttributeAsKey('name') 
        ->requiresAtLeastOneElement() 
        ->prototype('array') 
         ->children() 
          ->requiresAtLeastOneElement() 
          ->prototype('scalar') 
          ->end() 
         ->end() 
        ->end() 
       ->end() 
      ->end(); 

     return $treeBuilder; 
    } 
} 

내 매개 변수 :(
어떤 생각을 액세스 할 수없는거야? 감사합니다.

+0

S4에서는 더 이상 AppBundle이 없으므로이 방법은 작동하지 않습니다. 당신이 정말로 어떤 이유에서 그것을 필요로한다면 S3처럼 AppBundle을 만드십시오. – Cerad

답변

5

사용자 지정 구성 파일을로드하여 확장 클래스 ()를 사용하여 매개 변수를 처리하려면 (이 컴파일되기 전에 번들을 만들려면 결국)을 추가하고 메서드에 수동으로 Extension 클래스를 등록 할 수 있습니다.

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader) 
    { 
     # to avoid the same error you need to put this line at the top if your file is stored under "$this->getProjectDir().'/config'" directory 
     $container->registerExtension(new YourAppExtensionClass()); 

     #----- rest of the code 
    } 

그럼 당신은 평소 registering a Compiler Pass로 PARAMS를 사용할 수 있습니다.

희망이 도움이됩니다.

+1

전에 보지 못했지만 실제로 작동합니다. 번들 외부에 확장을 등록하는 방법에 대한 문서 : http://symfony.com/doc/current/components/dependency_injection/compilation.html – Cerad

+0

링크에 대한 @Cerad 감사 (처음에는 대답을 매우 빠르게 작성했습니다 _). 개인적으로 확장 클래스의 소스 코드를 보는 방법을 발견했습니다. (보통 Symfony가 어떻게 내부적으로 작동 하는지를 배우기 위해 _) :-). 추신 : 내 대답 (_my 영어 너무 좋아 _)을 향상 시키십시오. (_also 문법 _). –

+0

이 질문을 살펴볼 수도 있습니다. https://stackoverflow.com/questions/47915952/symfony-dependency-injection-not-triggering-load-function?noredirect=1#comment82836242_47915952 – Cerad