defaultValue()<\/em> method. More advanced validation can also be added, such as testing if a value is a string or in an array.<\/p>\nThis example shows defining a few different configuration settings, including child configuration, basic validation, and default values.<\/p>\n
<?php\r\nnamespace Acme\\AppBundle\\DependencyInjection;\r\n\r\nuse Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder;\r\nuse Symfony\\Component\\Config\\Definition\\ConfigurationInterface;\r\n\r\n\/**\r\n * This is the class that validates and merges configuration from your app\/config files\r\n *\r\n * To learn more see {@link http:\/\/symfony.com\/doc\/current\/cookbook\/bundles\/extension.html#cookbook-bundles-extension-config-class}\r\n *\/\r\nclass Configuration implements ConfigurationInterface\r\n{\r\n \/**\r\n * {@inheritDoc}\r\n *\/\r\n public function getConfigTreeBuilder()\r\n {\r\n $treeBuilder = new TreeBuilder();\r\n $rootNode = $treeBuilder->root('acme_app');\r\n\r\n $rootNode\r\n ->children()\r\n ->scalarNode('my_scaler_node')\r\n ->defaultValue('A sensible default value')\r\n ->end()\r\n ->booleanNode('my_boolean_node')\r\n ->defaultTrue()\r\n ->end()\r\n ->enumNode('my_enum_node')\r\n ->values(array('option_a', 'option_b', 'option_c'))\r\n ->isRequired()\r\n ->end()\r\n ->arrayNode('my_array_node')\r\n ->children()\r\n ->scalarNode('child_setting_one')->end()\r\n ->scalarNode('child_setting_two')->end()\r\n ->scalarNode('child_setting_three')->end()\r\n ->end()\r\n ->end()\r\n ->end()\r\n ;\r\n\r\n return $treeBuilder;\r\n }\r\n}\r\n<\/pre>\n<\/p>\n <\/p>\n
Setting Configuration Options<\/h4>\n
\nThis is how you would configure these options in app\/config\/config.yml<\/em> file.<\/p>\n# app\/config\/config.yml\r\n\r\nacme_app:\r\n my_scaler_node: \"A configured value\"\r\n my_boolean_node: false\r\n my_enum_node: \"option_b\"\r\n my_array_node:\r\n child_setting_one: \"child configuration setting one\"\r\n child_setting_two: \"child configuration setting two\"\r\n child_setting_three: \"child configuration setting three\"<\/pre>\n<\/p>\n <\/p>\n
Documenting the Configuration<\/h4>\n
\nAfter creating this configuration, full configuration definition documentation can be viewed using the app\/console config:dump-reference<\/em> terminal command. This will show the default bundle configuration in yml, including whether nodes are required to be set. The configuration above looks like this.<\/p>\n$ app\/console config:dump-reference acme_app\r\nDefault configuration for extension with alias: \"acme_app\"\r\nacme_app:\r\n my_scaler_node: A sensible default value\r\n my_boolean_node: true\r\n my_enum_node: ~ # Required\r\n my_array_node:\r\n child_setting_one: ~\r\n child_setting_two: ~\r\n child_setting_three: ~<\/pre>\n<\/p>\n <\/p>\n
Dependency Injection Extension Class<\/h2>\n
\nThe dependency injection extension class allows your bundle to load service configuration, and this is where you can access the configuration options that are set in the main config file, ie. app\/config\/config.yml<\/em>. This is where you can take action based on the configuration options. For instance, you can convert the configuration into different parameters that can then be injected into services or you can conditionally load additional configuration files based on the configuration values.<\/p>\nBelow, the load()<\/em> method is merging the configuration, accessing that configuration and setting parameters that can later be used by other services.<\/p>\n<?php\r\nnamespace Acme\\AppBundle\\DependencyInjection;\r\n\r\nuse Symfony\\Component\\DependencyInjection\\ContainerBuilder;\r\nuse Symfony\\Component\\Config\\FileLocator;\r\nuse Symfony\\Component\\HttpKernel\\DependencyInjection\\Extension;\r\nuse Symfony\\Component\\DependencyInjection\\Loader;\r\n\r\n\/**\r\n * This is the class that loads and manages your bundle configuration\r\n *\r\n * To learn more see {@link http:\/\/symfony.com\/doc\/current\/cookbook\/bundles\/extension.html}\r\n *\/\r\nclass AcmeAppExtension extends Extension\r\n{\r\n \/**\r\n * {@inheritDoc}\r\n *\/\r\n public function load(array $configs, ContainerBuilder $container)\r\n {\r\n $configuration = new Configuration();\r\n $config = $this->processConfiguration($configuration, $configs);\r\n\r\n $this->updateContainerParameters($container, $config);\r\n\r\n \/\/ Load before the configuration parameters get set.\r\n $loader = new Loader\\YamlFileLoader($container, new FileLocator(__DIR__.'\/..\/Resources\/config'));\r\n $loader->load('services.yml');\r\n }\r\n\r\n \/**\r\n * Update parameters using configuratoin values.\r\n *\r\n * @param ContainerBuilder $container\r\n * @param array $config\r\n *\/\r\n protected function updateContainerParameters(ContainerBuilder $container, array $config)\r\n {\r\n \/\/ Set a parameter for each configuration value\r\n $container->setParameter('acme_app.my_scaler_node', $config['my_scaler_node']);\r\n $container->setParameter('acme_app.my_boolean_node', $config['my_boolean_node']);\r\n $container->setParameter('acme_app.my_enum_node', $config['my_enum_node']);\r\n $container->setParameter('acme_app.my_array_node.child_setting_one', $config['my_array_node']['child_setting_one']);\r\n $container->setParameter('acme_app.my_array_node.child_setting_two', $config['my_array_node']['child_setting_two']);\r\n $container->setParameter('acme_app.my_array_node.child_setting_three', $config['my_array_node']['child_setting_three']);\r\n }\r\n}\r\n<\/pre>\n <\/p>\n
Another option is to conditionally load an additional configuration file that contains more service and parameter definitions. This file could override some default services or redefine a number of parameters, depending on what you need to do. This example shows how to load an optional services file based on the value of a configuration option.<\/p>\n
<?php\r\nnamespace Acme\\AppBundle\\DependencyInjection;\r\n\r\nuse Symfony\\Component\\DependencyInjection\\ContainerBuilder;\r\nuse Symfony\\Component\\Config\\FileLocator;\r\nuse Symfony\\Component\\HttpKernel\\DependencyInjection\\Extension;\r\nuse Symfony\\Component\\DependencyInjection\\Loader;\r\n\r\n\/**\r\n * This is the class that loads and manages your bundle configuration\r\n *\r\n * To learn more see {@link http:\/\/symfony.com\/doc\/current\/cookbook\/bundles\/extension.html}\r\n *\/\r\nclass AcmeAppExtension extends Extension\r\n{\r\n \/**\r\n * {@inheritDoc}\r\n *\/\r\n public function load(array $configs, ContainerBuilder $container)\r\n {\r\n $configuration = new Configuration();\r\n $config = $this->processConfiguration($configuration, $configs);\r\n\r\n \/\/ Load before the configuration parameters get set.\r\n $loader = new Loader\\YamlFileLoader($container, new FileLocator(__DIR__.'\/..\/Resources\/config'));\r\n $loader->load('services.yml');\r\n \r\n if ($config['my_boolean_node']) {\r\n\r\n $loader->load('optional_services.yml');\r\n }\r\n }\r\n}<\/pre>\n<\/p>\n <\/p>\n
Supporting XML<\/h2>\n
\nThere are some additonal steps that need to be taken to support the use of XML configuration files. You’ll need to define a namespace and create an XML schema. More information on that can be found here<\/a>.\n<\/p>\n <\/p>\n
Conclusion<\/h2>\n
\nIf your bundle does not rely heavily on parameters and will not be reused then a simple parameter may be all you need. Exposing semantic configuration in Symfony is a little more involved than simply using parameters, but if you intend to share or reuse your bundle on multiple projects then it is well worth the additional effort.<\/p>\n","protected":false},"excerpt":{"rendered":"
In Symfony 2 there are two main ways to define configuration options for a bundle. The first way is to simply define parameters in the service container and the second is to expose semantic configuration for your bundle.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[4],"_links":{"self":[{"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/posts\/593"}],"collection":[{"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/comments?post=593"}],"version-history":[{"count":16,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/posts\/593\/revisions"}],"predecessor-version":[{"id":643,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/posts\/593\/revisions\/643"}],"wp:attachment":[{"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/media?parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/categories?post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/tags?post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}