S
S
sl1m_dogg2016-02-22 14:00:22
symfony
sl1m_dogg, 2016-02-22 14:00:22

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

2 answer(s)
S
sl1m_dogg, 2016-02-22
@sl1m_dogg

<?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')]);
    }
}

B
BoShurik, 2016-02-22
@BoShurik

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')]));
    }

Use PhpStorm, it protects against such errors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question