Last active
December 22, 2015 15:28
-
-
Save fracasula/6492483 to your computer and use it in GitHub Desktop.
Symfony2 Bundle basic parameters configuration tree (config.yml)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Bundle\Bundlename\DependencyInjection; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\Config\FileLocator; | |
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | |
use Symfony\Component\DependencyInjection\Loader; | |
/** | |
* This is the class that loads and manages your bundle configuration | |
* | |
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} | |
*/ | |
class AcmeBundlenameExtension extends Extension | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function load(array $configs, ContainerBuilder $container) | |
{ | |
$configuration = new Configuration(); | |
$config = $this->processConfiguration($configuration, $configs); | |
foreach ($config as $setting => $configurationValue) | |
foreach ($configurationValue as $k => $v) | |
$container->setParameter('acme_bundlename.' . $setting . '.' . $k, $v); | |
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | |
$loader->load('services.yml'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/config/config.yml | |
acme_bundlename: | |
param1: | |
param1_child1: "value" | |
param1_child2: "value" | |
param2: | |
param2_child1: "value" | |
param2_child2: "value" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Bundle\Bundlename\DependencyInjection; | |
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | |
use Symfony\Component\Config\Definition\ConfigurationInterface; | |
/** | |
* This is the class that validates and merges configuration from your app/config files | |
* | |
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} | |
* | |
* Adding node definitions to the tree and Node types {@link http://symfony.com/doc/master/components/config/definition.html} | |
*/ | |
class Configuration implements ConfigurationInterface | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getConfigTreeBuilder() | |
{ | |
$treeBuilder = new TreeBuilder(); | |
$rootNode = $treeBuilder->root('acme_bundlename'); | |
$rootNode | |
->children() | |
->arrayNode('param1') | |
->children() | |
->scalarNode('param1_child1')->isRequired()->end() | |
->scalarNode('param1_child2')->isRequired()->end() | |
->end() | |
->end() | |
->arrayNode('param2') | |
->children() | |
->scalarNode('param2_child1')->isRequired()->end() | |
->scalarNode('param2_child2')->isRequired()->end() | |
->end() | |
->end() | |
->end() | |
; | |
return $treeBuilder; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Bundle\Bundlename\DependencyInjection; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
class DefaultController extends Controller | |
{ | |
public function indexAction() | |
{ | |
$this->container->getParameter('acme_bundlename.param1.param1_child1'); // "value" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment