Creating configurations in Symfony - php

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 Symfony documentation on configuration files and with every article or other piece of information I read, it gets harder and harder to understand.
Details
I've created my own Bundle. Lets call it HappyBundle. I've put this Bundle in my company's folder. So naturally I've got CompanyHappyBundle.
I want to make a configuration file specifically for this bundle as I want it to be reusable.
As I test i created the following:
# src/Company/HappyBundle/Resources/config/config.yml
company_happy:
import:
path: /tmp
Now, what I want is to be able to use this value in my Controller. I just don't know how. It throws me the following error:
[Symfony\Component\Config\Exception\FileLoaderLoadException]
There is no extension able to load the configuration for "company_happy" (in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml).
Looked for namespace "company_happy", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution" in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml (which is being imported from "/home/user/symfony/app/config/config.yml").
Update
In the config.yml I added the following:
#app/config/config.yml
imports:
- { resource: "#CompanyHappyBundle/Resources/config/config.yml" }
I've also made a Configuration class because I read somewhere this was required. I really do think this is alot of work to make just one config file.
namespace Company\HappyBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {#inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('company_happy');
$rootNode
->children()
->arrayNode('import')
->children()
->scalarNode('attachments_path')->defaultValue('/tmp')->end()
->scalarNode('method')->defaultValue('ALL')->end()
->booleanNode('move_mail')->defaultValue(true)->end()
->booleanNode('mark_read')->defaultValue(true)->end()
->end()
->end()
;
return $treeBuilder;
}
}
What I am actually looking for are the steps and requirements needed to get this working. The thing with symfony is that it has a million ways to do this. The documentation doesn't just give a workflow.
Can someone please help me out?

I have solved my own issue but not without trouble. I'm not at all pleased with Symfony's configuration system.
Step one - Create your config file
Create a file named config.yml in src/<bundle name>/Resources/config/
yourbundle:
param_one: value_one
param_two: value_two
param_three: value_three
param_four: value_four
param_five:
subparam_one: subvalue_one
subparam_two: subvalue_two
subparam_three: subvalue_three
subparam_four: subvalue_four
Step two - Importing your configuration file
Go to app/config/config.yml and add:
#app/config/config.yml
imports:
- { resource: "#YourBundle/Resources/config/config.yml" }
Step three - Create a configuration class
Create a file named Configuration.php in src/<bundle name>/DependencyInjection/
namespace YourBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {#inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('yourbundle');
$rootNode
->children()
->scalarNode('param_one')->defaultValue('value_one')->end()
->scalarNode('param_two')->defaultValue('value_two')->end()
->scalarNode('param_three')->defaultValue('value_three')->end()
->scalarNode('param_four')->defaultValue('value_four')->end()
->arrayNode('param_five')
->children()
->scalarNode('subparam_one')->defaultValue('subvalue_one')->end()
->scalarNode('subparam_two')->defaultValue('subvalue_two')->end()
->scalarNode('subparam_three')->defaultValue('subvalue_three')->end()
->scalarNode('subparam_four')->defaultValue('subvalue_four')->end()
->end()
->end()
;
return $treeBuilder;
}
}
Step four - Creating an extension
Last but not least, you'll have to create an extension. Create a file <yourbundle>Extension.php in src/<your bundle>/DependencyInjection/
namespace YourBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
class YourbundleExtension extends Extension
{
/**
* #var ContainerBuilder
*/
protected $container;
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$this->container = $container;
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
foreach ($config as $key => $value) {
$this->parseNode('yourbundle.'.$key, $value);
}
$container->setParameter('yourbundle', $config);
}
/**
* #param string $name
* #param mixed $value
*
* #throws \Exception
*/
protected function parseNode($name, $value)
{
if (is_string($value)) {
$this->set($name, $value);
return;
}
if (is_integer($value)) {
$this->set($name, $value);
return;
}
if (is_array($value)) {
foreach ($value as $newKey => $newValue) {
$this->parseNode($name.'.'.$newKey, $newValue);
}
return;
}
if (is_bool($value)) {
$this->set($name, $value);
return;
}
throw new \Exception(gettype($value).' not supported');
}
/**
* #param string $key
* #param mixed $value
*/
protected function set($key, $value)
{
$this->container->setParameter($key, $value);
}
}
All these steps are required just to be able to call a configuration parameter specific for your bundle.
If any of you know any way to do this easier, feel free to post an answer or comment.

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:
<?php
namespace Company\Bundle\HappyBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class CompanyHappyBundle extends Bundle
{
}
src/Company/Bundle/HappyBundle/DependencyInjection/CompanyHappyExtension.php
<?php
namespace Company\Bundle\HappyBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
class CompanyHappyExtension extends Extension implements ExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$configuration = new Configuration();
$options = $this->processConfiguration($configuration, $configs);
// Do something with your options here
}
}
src/Company/Bundle/HappyBundle/DependencyInjection/Configuration.php
<?
namespace Company\Bundle\HappyBundle\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('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:

you're almost done, you just need to configure your Bundle to use your config parameters, take a look at this answer.

Related

Symfony 3.1 - create global parameter for AppBundle

I'm trying to define some parameters inside the AppBundle without success. It's one of my first test on this framework so there may be obvious concept that I misunderstood.
The first problem is the field bundle_version which is not read inside the YAML file. The error returned is the following :
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
The child node "bundle_version" at path "costs" must be configured.
When i comment this line, i get another error :
[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
There is no extension able to load the configuration for "costs" (in W:\wamp64\www\test\src\AppBundle\DependencyInjecti
on/../Resources/config\costs.yml). Looked for namespace "costs", found none
src/AppBundle/DependencyInjection/Configuration.php
namespace AppBundle\DependencyInjection;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('costs');
$rootNode->children()
// THIS LINE DON'T WORK AND CAUSE THE 1st PROBLEM
->floatNode('bundle_version')->isRequired()->end()
->arrayNode('shipping')
->children()
->floatNode('es')->isRequired()->end()
->floatNode('it')->isRequired()->end()
->floatNode('uk')->isRequired()->end()
->end()
->end()
->arrayNode('services')
->children()
->floatNode('serviceA')->isRequired()->end()
->floatNode('serviceB')->isRequired()->end()
->end()
->end()
->end();
return $treeBuilder;
}
}
/src/AppBundle/Resources/config.costs.yml
costs:
bundle_version: 1.0 # THIS FIELD IS NOT RECOGNIZED
shipping:
es: 18.00
it: 19.00
uk: 20.00
services:
serviceA: 15.00
serviceB: 20.00
/src/AppBundle/DependencyInjection/AppExtension.php
namespace AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class AppExtension extends Extension
{
public function load(array $config, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('costs.yml');
}
}
The solution is quite simple as i expected ... In /app/config/config.yml there's a predefined "parameters" key and we're supposed to putt our params here if they're not Bundle specific.
So it's just something like :
# /app/config/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application- related-configuration
parameters:
locale: fr
my_param: my_value
Then in your controller :
// /src/AppBundle/Controller/DefaultController.php
class DefaultController extends Controller
{
/**
* #Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$this->getParameter('my_param'); // or $this->getContainer()->getParameter('my_param');
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
]);
}
}
Hope it helps

How can I define Optional dependecies for seperate bundles?

I will explain with an example:
There are 2 bundles : Foo\SecurityBundle and Foo\MenuBundle.
Foo\MenuBundle has Menu class that looks like this:
namespace Foo\MenuBundle;
use Foo\SecurityBundle\MenuSecurer; //note this
class Menu{
protected $securer;
public function __construct(MenuSecurer $securer = null){
$this->securer = $securer;
}
public function buildMenu(){
//build the $menu ...
//...
if($this->securer != null)
$securer->secure($menu);
}
}
the security bundle will automatically inject the $menuSecurer if it is installed,
however the problem is When the security bundle is not installed then it's classes aren't defined either, so i can't use Foo\SecurityBundle... in the MenuBundle even though I don't really use it.what's the correct way around this?
There is a section in the symfony docs dealing with this this situation:
http://symfony.com/doc/current/book/service_container.html#optional-dependencies-setter-injection
If you have optional dependencies for a class, then "setter injection"
may be a better option.
According to this your class might look like this:
namespace Foo\MenuBundle;
class Menu{
protected $securer;
public function setSecurer($securer) {
$this->securer = $securer;
}
public function buildMenu(){
//build the $menu ...
//...
if($this->securer != null)
$securer->secure($menu);
}
}
# config.yml
menu_service:
class: Foo\MenuBundle\Menu
calls:
- [setMailer, ["#securer"]]
Something like this...
Unfortunately you still cannot have a use statement without an interface you know is existing.
There are quite a few ways to go about this, but I think a good approach would be to add a configuration setting for the class/service to be injected into the constructors first argument.
For example, in the Foo\MenuBundle\Menu class (assuming this is defined as a service already), you could add an additional item to the bundle's configuration to define a default service for the $securer, then optionally override it in the config if desired.
In the configuration class (DependecyInjection\Configuration.php):
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('foo_menu');
$rootNode
->children()
->arrayNode('service')
->addDefaultsIfNotSet()
->children()
->scalarNode('menu_securer')->defaultValue('foo_security.menu_securer')->end()
>end()
->end()
->end();
return $treeBuilder;
}
In the extension class (DependecyInjection\FooMenuExtension.php):
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
// ...
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
// This is what sets 'foo_menu.menu_securer' to the service you want
foreach ($config['service'] as $key => $service) {
$container->setAlias($this->getAlias() . '.' . $key, $service);
}
$loader->load('services.xml');
$container->getDefinition('foo_menu.menu.menu')
->replaceArgument(0, new Reference('foo_menu.menu_securer'));
}
And your service definition would just look something like this...
<service id="foo_menu.menu.menu" class="%foo_menu.menu.menu.class%">
<argument /> <!-- foo_menu.menu_securer -->
</service>
Now in your config.yml you can just switch out the service you want to use by defining it in under...
foo_menu:
service:
menu_securer: 'some_other.service'
Edit: Regarding the type hinting, as mentioned by Markus, it would probably be a good idea to implement an interface that the $securer must implement.

Getting errors when validating Symfony2 semantic configuration

I'm trying to write a semantic configuration of the bundle in Symfony2. I have prepared a configuration file using YAML (spu.yml):
spu:
modules:
spu-module:
reuqires: []
path: modules/spu-module.js
Then I'm loading it in Extension file (DependencyInjection/spuExtension.php):
namespace Gig\SpuBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class spuExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration( $configuration, $configs );
$loader = new Loader\YamlFileLoader( $container, new FileLocator( __DIR__ . '/../Resources/config' ) );
$loader->load( 'services.yml' );
$loader->load( 'spu.yml' );
}
}
And configuration class ()DependencyInjection/Configuration.php) for this extension is:
namespace Gig\SpuBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root( 'spu' );
$rootNode
->children()
->arrayNode('modules')
->isRequired()
->prototype('array')
->end()
->end();
return $treeBuilder;
}
}
When validating the config I get an error:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
The child node "modules" at path "spu" must be configured.
What am I doing wrong?
You must include this in your main app/config/config.yml file
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: spu.yml }
If you want to have default parameters, set them in DependencyInjection/Configuration.php
This can be something like this.
$rootNode
->children()
->arrayNode('modules')
->isRequired()
->addDefaultsIfNotSet()
->prototype('array')
->children()
->arrayNode('reuqires')->end()
->scalarNode('path')->defaultValue('modules/spu-module.js')->end()
->end()
->end()
->end()
->end();
The best way to do this is to let the users define the configuration for your bundle in the config.yml file. There is no need for a special file.
You can keep the configuration which you have in your Configuration class but you will have to modify your spuExtension class.
The way Symfony works you will have to read the values from the config.yml for your bundle in the spuExtension class and then set them as parameters. Then you will have easy access to these values by using the container's getParameter method.
This is how your config.yml will look
spu:
modules:
spu-module:
reuqires: []
path: modules/spu-module.js
Then in your spuExtension class you can get these values specified by doing:
class spuExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration( $configuration, $configs );
$loader = new Loader\YamlFileLoader( $container, new FileLocator( __DIR__ . '/../Resources/config' ) );
$loader->load( 'services.yml' );
$container->setParameter('modules', $configs[0]);
}
}
This will make it easy for you to use the container in an controller to get a parameter.
The $configs variable in the spuExtension will contain all the configuration that is listed under your root. Which in this case is spu.
The way Symfony works you first have to load the configuration in your bundle's extension class and then set this as a parameter, this seems redundant but it's a best practice and it was intended to work like this.
Ok, I did it different way.
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader( $container, new FileLocator( __DIR__ . '/../Resources/config' ) );
$loader->load( 'services.yml' );
$loader->load( 'spu.yml' );
$spuConfig = Yaml::parse(__DIR__ . '/../Resources/config/spu.yml');
$configs = array_merge($configs, $spuConfig);
$configuration = new Configuration();
$config = $this->processConfiguration( $configuration, $configs );
}
And now I'm getting what I wanted. I can validate params in Configuration class.

Symfony 2 loading custom configuration file

I want to add a new configuration file in Bundle/Resources/config. I've tried following http://symfony.com/doc/current/cookbook/bundles/extension.html , but it doesn't work as it should and I get
There is no extension able to load the configuration for
"mailbroker_mail_details"
My files:
MailbrokerMailDetailsExtension.php
<?php
namespace Mailbroker\MailDetailsBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class MailbrokerMailDetailsExtension extends Extension
{
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load('canonisers.yml');
}
public function getAlias()
{
return 'mailbroker_mail_details';
}
}
Configuration.php
<?php
namespace Mailbroker\MailDetailsBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {#inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mailbroker_mail_details');
$rootNode
->children()
->scalarNode('abc')->end()
->end()
;
return $treeBuilder;
}
}
canonisers.yml
mailbroker_mail_details:
abc: 123
The Configuration is correct (when placed in app/config/config.yml it loads as it should), canonisers.yml is loaded correctly, but for some reason I can't make it work together. Thanks for your help!
Well, I have not tried it but you should be able to use the Yaml extension to load in the canonisers.yml file directly and add it to configs. Not recommended (bypasses the application caching stuff) but it might work:
use Symfony\Component\Yaml\Yaml;
class MailbrokerMailDetailsExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$file = __DIR__.'/../Resources/config/canonisers.yml';
$configs = array_merge($configs,Yaml::parse(file_get_contents($file));
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
....
Completely untested. You might need to add to app/config/config.yml
mailbroker_mail_details: ~
Just to get past the error message. Not sure.
Let me know if it works.
Ok, so #Iltar from #symfony irc channel pointed me to cookbook: http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html
Long story short, PrependExtensionInterface with prepend method.
It was added since I last read through symfony book and cookbook, and it wasn't exactly googlable in this case, so I'll just leave the link here for other people.

How to expose semantic configuration

In the process of building a third party bundle for the Symfony2 framework I have run into a problem with allowing configuration to happen in the app/config.yml file. I want to do this so users of the bundle (mostly myself) won't have to go into the bundle to make configuration changes.
My bundle Configuration.php file reads:
<?php
namespace Ms2474\AuthNetBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
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('authorize_net');
$rootNode
->children()
->scalarNode('login_id')->defaultNull()->end()
->scalarNode('transaction_key')->defaultNull()->end()
->booleanNode('sandbox')->defaultValue(true)->end()
->scalarNode('log_file')->defaultValue(false)->end()
->end();
return $treeBuilder;
}
}
The bundle extension file (Ms2474AuthNetBundleExtension.php) reads:
<?php
namespace Ms2474\AuthNetBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Processor;
class Ms2474AuthNetBundleExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->process($configuration->getConfigTree(), $configs);
if (null === $config['sandbox']) {
$config['sandbox'] = $container->getParameter('kernel.debug');
}
$container->setParameter('authorize_net.login_id', $config['login_id']);
$container->setParameter('authorize_net.transaction_key', $config['transaction_key']);
$container->setParameter('authorize_net.sandbox', $config['sandbox']);
if (isset($config['log_file'])) {
$container->setParameter('authorize_net.log_file', $config['log_file']);
}
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
Now for the problem:
When I try to add the following to the app/config.yml file:
authorize_net:
login_id: 1234
transaction_key: 1234
sandbox: true
log_file: false
I get the following two errors:
InvalidArgumentException: There is no extension able to load the configuration for "authorize_net" (in /path/to/app/config/config.yml). Looked for namespace "authorize_net", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "jms_security_extra", "jms_aop", "fos_user", "jms_serializer", "fos_rest", "stof_doctrine_extensions", "vich_uploader", "gri_user", "gri_campaign", "gri_authorized_contact", "web_profiler", "sensio_distribution"
and:
FileLoaderLoadException: Cannot import resource "/path/to/app/config/config.yml" from "/path/to/app/config/config_dev.yml".
And the question:
What am I doing wrong here? I have looked through the documentation and also compared my code to other bundles such as the FOSUserBundle.
First, it should be called Ms2474AuthNetExtension, not Ms2474AuthNetBundleExtension.
Second, unless you've set up a custom namespace for the extension and loaded it manually, the configuration options should be under ms2474_auth_net, not authorize_net.

Categories