Answer the question
In order to leave comments, you need to log in
What is wrong with my extensions in symfony3?
I declared two services with dependencies, I write ./bin/console debug:container and I see both services in the container, but when I start the controller, it writes null given. Code:
ideone.com/VQ65jr
Answer the question
In order to leave comments, you need to log in
<?php
namespace MailerBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class MailerExtension extends Extension
{
/**
* Loads a specific configuration.
*
* @param array $configs An array of configuration values
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$container->setDefinition('emailsender', new Definition('MailerBundle\Sender\EmailSender'),[
new Reference('swiftmailer.mailer'),
new Reference('swiftmailer.transport')]);
$container->setDefinition('emailnotifier', new Definition('MailerBundle\Controller\EmailController'),[
new Reference('emailsender')]);
}
}
Class parameters are passed in the Definition constructor, not in the setDefinition method
public function load(array $configs, ContainerBuilder $container)
{
$container->setDefinition('emailsender', new Definition('MailerBundle\Sender\EmailSender', [
new Reference('swiftmailer.mailer'),
new Reference('swiftmailer.transport')]));
$container->setDefinition('emailnotifier', new Definition('MailerBundle\Controller\EmailController', [
new Reference('emailsender')]));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question