Creating configurations in Symfony

前端 未结 3 1631
别跟我提以往
别跟我提以往 2020-12-30 15:25

For a few hours now I\'ve been struggling to do the most simple thing you can imagine and it just won\'t work. I\'ve read tons of stackoverflow questions, read the complete

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 15:58

    A few notices:

    In config.yml, you are trying to define import as array. It seems like symfony doesn't allow creating array elements in the root of your config, meaning that you have to nest arrays deeper down the tree. So you can not do:

    company_happy:
        import:
            path: /tmp
        another_import:
            ...
    

    I am not sure this is exactly what you were trying to do, but you defined import as array, which makes me assume so.

    On the other hand, you can do:

    company_happy:
        imports:
            import:
                path: /tmp
            another_import:
                ...
    

    Regarding no extension able to load the configuration error: Make sure your extension file is following naming convetions.It should be called CompanyHappyExtension.php with CompanyHappyExtension class defined inside.

    I have created a sample CompanyHappyBundle bundle which is working fine on Symofny 3 (probably works on S2 as well). Feel free to clone/download it :)

    The services.yml file is an added bonus, as you will most likely need it anyway.

    src/Company/Bundle/HappyBundle/CompanyHappyBundle.php:

    src/Company/Bundle/HappyBundle/DependencyInjection/CompanyHappyExtension.php

    load('services.yml');
    
            $configuration = new Configuration();
            $options = $this->processConfiguration($configuration, $configs);
    
            // Do something with your options here
        }
    
    
    }
    

    src/Company/Bundle/HappyBundle/DependencyInjection/Configuration.php

    root('company_happy');
    
            $rootNode
                ->children()
                    ->arrayNode('imports')
                        ->prototype('array')
                        ->children()
                            ->scalarNode('path')->defaultValue('/tmp')->end()
                            ->scalarNode('method')->defaultValue('ALL')->end()
                            ->booleanNode('move_mail')->defaultValue(true)->end()
                            ->booleanNode('mark_read')->defaultValue(true)->end()
                        ->end()
                    ->end()
                ->end()
            ;
    
            return $treeBuilder;
        }
    }
    

    src/Company/Bundle/HappyBundle/Resources/config/config.yml

    company_happy:
        imports:
            import:
                path: /tmp
    

    src/Company/Bundle/HappyBundle/Resources/config/services.yml

    # Define your services here
    services:
    

提交回复
热议问题