Symfony Fosuserbundle 重新发送电子邮件

Symfony Fosuserbundle resend email

我正在尝试制作一个 link 在 Symfony3 中注册后向用户重新发送确认令牌。

但是,我收到如下弃用消息:

User Deprecated: The "fos_user.mailer" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.

这是我的控制器:

public function resendActivationEmail($token, UserManagerInterface $userManager)
{
    $user = $userManager->findUserByConfirmationToken($token);
    if (is_null($user)) {return $this->redirectToRoute('fos_user_registration_register');}
    $mailer = $this->get('fos_user.mailer');
    $mailer->sendConfirmationEmailMessage($user);
    return $this->redirectToRoute('fos_user_registration_check_email');
}

我的services.yml:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: truesubscribers, etc.
        autoconfigure: true
        public: false

我查看了文档,它说在 Symfony3.4 中,服务默认是私有的。我在我的应用程序中使用自动装配,那么如何在没有任何弃用警告的情况下获得 fos_user.mailer

我尝试将 Fosuserbundle 服务设置为 public,没有帮助:

services.yml:

    ....
    FOS\UserBundle:
            public: true

感谢任何帮助!

使用$mailer = $this->container->get('fos_user.mailer');

而不是$mailer = $this->get('fos_user.mailer');

最好使用DependencyInjection而不是直接调用容器。您应该将邮件程序传递给您的方法:

public function resendActivationEmail($token, UserManagerInterface $userManager, \FOS\UserBundle\Mailer\MailerInterface $mailer)
{
    $user = $userManager->findUserByConfirmationToken($token);
    if (is_null($user)) {return $this->redirectToRoute('fos_user_registration_register');}
    $mailer->sendConfirmationEmailMessage($user);
    return $this->redirectToRoute('fos_user_registration_check_email');
}

有关 dependencyInjection 的更多信息:https://symfony.com/doc/3.4/service_container/injection_types.html