S
S
sl1m_dogg2016-03-21 23:14:43
symfony
sl1m_dogg, 2016-03-21 23:14:43

Services are not loaded via [bundle]Extension.php, what am I doing wrong?

I did everything according to the symphony docks. The actual code:
MailerExtension.php:

<?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('mailer.emailsender', new Definition('MailerBundle\Sender\EmailSender',[
            new Reference('swiftmailer.mailer'),
            new Reference('swiftmailer.transport'),
            $container->getParameter('mailer_user'),
            $container->getParameter('mailer_password')
        ]));

        $container->setDefinition('mailer.email_controller', new Definition('MailerBundle\Controller\EmailController',[
            new Reference('mailer.emailsender')]));
    }
}

Sender\EmailSender.php:
<?php

namespace MailerBundle\Sender;

use MailerBundle\Entity\Notification;

class EmailSender extends AbstractSender
{
    protected $transport;

    /**
     * EmailSender constructor.
     * @param \Swift_Mailer $mailer
     * @param \Swift_SmtpTransport $smtpTransport
     * @param $name
     * @param $password
     */
    public function __construct(
        \Swift_Mailer $mailer,
        \Swift_SmtpTransport $smtpTransport,
        $name,
        $password
    )
    {
        $transport = $smtpTransport::newInstance('gmail')
            ->setUsername($name)
            ->setPassword($password);

        $this->transport = $mailer::newInstance($transport);
    }

EmailController.php:
<?php

namespace MailerBundle\Controller;

use MailerBundle\Entity\EmailHandler\EmailReceiver;
use MailerBundle\Entity\EmailHandler\EmailSender;
use MailerBundle\Sender\EmailSender as Sender;
use MailerBundle\Entity\Notification;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class EmailController extends Controller
{
    /**
     * @var Sender
     */
    private $sender;

    /**
     * EmailController constructor.
     * @param Sender $sender
     */
    public function __construct(Sender $sender)
    {
        $this->sender = $sender;
    }

When switching to the controller route, it issues:
Catchable Fatal Error: Argument 1 passed to MailerBundle\Controller\EmailController::__construct() must be an instance of MailerBundle\Sender\EmailSender, none given, called in C:\xampp1\htdocs\murka.ll\app\cache\dev\classes.php on line 2176 and defined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2016-03-22
@prototype_denis

Because in this case, the controller must be declared a service.
symfony.com/doc/current/cookbook/controller/servic...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question