Symfony:必须是Twig_Environment的实例

时间:2018-12-22 19:00:34

标签: php symfony

我正在尝试为已经制作好的Symfony应用实现密码重置功能。我想我快要到那里了...但是我对Symfony还是很陌生,我仍在搜索自己的自已信息...我有一个视图将JSON帖子发送到控制器,但它返回以下消息:

Type error: Argument 1 passed to AppBundle\Event\ForgotPasswordEventListener::__construct() must be an instance of Twig_Environment, instance of Doctrine\ORM\EntityManager given, called in /home/***/public_html/intranet/var/cache/prod/ContainerJxqbmi7/getApp_Listener_ForgotPasswordService.php on line 8

这是我的控制者:

<?php

namespace AppBundle\Event;
use CoopTilleuls\ForgotPasswordBundle\Event\ForgotPasswordEvent;

class ForgotPasswordEventListener
{

    private $twig;
    private $mailer;
    private $manager;

    public function __construct(\Twig_Environment $twig, \Swift_Mailer $mailer, \Doctrine\ORM\EntityManager $manager)
    {
        $this->templating = $twig;
        $this->mailer = $mailer;
        $this->manager=$manager;
    }

    /**
     * @param ForgotPasswordEvent $event
     */
    public function onCreateToken(ForgotPasswordEvent $event)
    {
        $passwordToken = $event->getPasswordToken();
        $user = $passwordToken->getUser();

        $swiftMessage = new \Swift_Message(
            'Reset of your password',
            $this->templating->render(
             'security/forgot-password.html.twig',
                [
                    'reset_password_url' => sprintf('http://www.example.com/forgot-password/%s', $passwordToken->getToken()),
                ]
            )
        );

        $swiftMessage->setFrom('no-reply@example.com');
        $swiftMessage->setTo($user->getEmail());
        $swiftMessage->setContentType('text/html');
        if (0 === $this->mailer->send($swiftMessage)) {
            throw new \RuntimeException('Unable to send email');
        }
    }

    /**
     * @param ForgotPasswordEvent $event
     */

    public function onUpdatePassword(ForgotPasswordEvent $event)
    {
        $passwordToken = $event->getPasswordToken();
        $user = $passwordToken->getUser();
        $user->setPlainPassword($event->getPassword());
        $this->manager->persist($user);
    }
}

这是我的服务

app.listener.forgot_password:
    class: AppBundle\Event\ForgotPasswordEventListener
    arguments:
        templating: "@twig"
        mailer:     "@mailer"
        manager:    "@doctrine.orm.entity_manager"
    tags:
        - { name: kernel.event_listener, event: coop_tilleuls_forgot_password.create_token, method: onCreateToken}
        - { name: templating.helper, alias: templating, event: coop_tilleuls_forgot_password.create_token, method: onCreateToken}
        - { name: kernel.event_listener, event: coop_tilleuls_forgot_password.update_password, method: onUpdatePassword }

我真的不知道我在做什么错。谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

问题是您的服务定义。我不确定到底是什么原因造成的,但似乎Symfony正在重新排列参数。有2种解决方法。要么删除参数的键,要么通过使用命名属性正确地对其进行修复:

app.listener.forgot_password:
    class: AppBundle\Event\ForgotPasswordEventListener
    arguments:
        - "@twig"
        - "@mailer"
        - "@doctrine.orm.entity_manager"

或第二种方法:

app.listener.forgot_password:
    class: AppBundle\Event\ForgotPasswordEventListener
    arguments:
        $twig: "@twig"
        $mailer: "@mailer"
        $manager: "@doctrine.orm.entity_manager"

重要的一点是$前缀,并且该名称与构造函数中的参数名称匹配。