【问题标题】:Cannot autowire service in Symfony 3.4 and FosUserBundle无法在 Symfony 3.4 和 FosUserBundle 中自动装配服务
【发布时间】:2019-02-24 20:34:45
【问题描述】:

我尝试在注册新用户后覆盖 FosUserBundle 中的 REGISTRATION_SUCCESS 以重定向用户列表中的管理员。

所以我创建了一个新的事件订阅者:

<?php
namespace AppBundle\EventListener;

use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class RedirectAfterRegistrationSubscriber implements EventSubscriberInterface
{
    private $mailer;
    private $tokenGenerator;
    private $router;

    public function __construct(MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator, UrlGeneratorInterface $router)
    {
        $this->mailer = $mailer;
        $this->tokenGenerator = $tokenGenerator;
        $this->router = $router;
    }

    public function onRegistrationSuccess(FormEvent $event)
    {
        $user = $event->getForm()->getData();

        $user->setEnabled(false);
        if (null === $user->getConfirmationToken()) {
            $user->setConfirmationToken($this->tokenGenerator->generateToken());
        }

        $this->mailer->sendConfirmationEmailMessage($user);

        $url = $this->router->generate('user_index');
        $event->setResponse(new RedirectResponse($url));
    }

    public static function getSubscribedEvents()
    {
        return [
            FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'
        ];
    }
}

以及以下服务:

app.redirect_after_registration_subscriber:
    class: AppBundle\EventListener\RedirectAfterRegistrationSubscriber
    arguments: ['@fos_user.mailer', '@fos_user.util.token_generator', '@router']
    tags:
        - { name: kernel.event_subscriber }

我不明白为什么会出现这个错误:

Cannot autowire service "AppBundle\EventListener\RedirectAfterRegistrationSubscriber": 
argument "$mailer" of method "__construct()" references interface 
"FOS\UserBundle\Mailer\MailerInterface" but no such service exists. You should maybe alias
this interface to one of these existing services: "fos_user.mailer.default",
"fos_user.mailer.twig_swift", "fos_user.mailer.noop".

【问题讨论】:

  • 你试过'@fos_user.mailer.twig_swift而不是'@fos_user.mailer'吗?
  • 你的答案在错误信息You should maybe alias this interface to one of these existing services: "fos_user.mailer.default", "fos_user.mailer.twig_swift", "fos_user.mailer.noop".你试过了吗?
  • 感谢您的回答。 @Domagoj:我尝试过使用 '@fos_user.mailer.twig_swift' 但问题仍然存在。
  • @Preciel :如果别名服务包括添加 services.yml :fos_user.mailer 别名:'@fos_user.mailer.twig_swift' 并用'@fos_user 替换'@fos_user.mail.twig_swift'。 mailer' 在服务中,我做到了,但消除了同样的错误:(

标签: symfony fosuserbundle


【解决方案1】:

我想您正在使用服务的自动发现。比如:

# services.yaml

AppBundle\:
    resource: '../src/'
...

所以除了你定义的@app.redirect_after_registration_subscriber,Symfony 还定义了另一个ID 为@AppBundle\EventListener\RedirectAfterRegistrationSubscriber 的服务。两者都指向AppBundle\EventListener\RedirectAfterRegistrationSubscriber 类。但是您只为第一个配置了 mailer 参数。

解决办法:

AppBundle\EventListener\RedirectAfterRegistrationSubscriber:
    arguments: ['@fos_user.mailer', '@fos_user.util.token_generator', '@router']
    tags:
        - { name: kernel.event_subscriber }

通过自动装配和自动配置,您甚至可以将其指定为:

AppBundle\EventListener\RedirectAfterRegistrationSubscriber:
    arguments:
        $mailer: '@fos_user.mailer'

【讨论】:

  • 问题解决了!似乎它只适用于你给我的代码,所以我的问题是,我是否也需要添加我的服务,还是只需要你的服务?非常感谢!
  • app.redirect_after_registration_subscriber不需要了,你应该删除它。
  • 好的,谢谢!我还有另一个问题,在我的配置中,管理员创建用户帐户,然后向用户发送电子邮件。因此,用户在单击确认电子邮件时未登录网站。点击链接后,他被重定向到登录表单而不是直接确认页面。如果可能的话,我将能够自动记录用户。是否可以覆盖registration_confirm或registration_confirmed监听器??
猜你喜欢
  • 2018-11-02
  • 2018-10-10
  • 2018-08-05
  • 2019-02-28
  • 2018-11-21
  • 2019-03-03
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多