简体   繁体   English

Symfony Fosuserbundle重新发送电子邮件

[英]Symfony Fosuserbundle resend email

I am trying to make a link that resends a confirmation token to a user after registering in Symfony3. 我正在尝试建立一个链接,该链接在Symfony3中注册后向用户重新发送确认令牌。

However, I get a deprecation message as follows: 但是,我收到以下弃用消息:

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. 不推荐使用的用户:“ fos_user.mailer”服务是私有的,从Symfony 3.2开始不推荐从容器中获取它,并且在4.0中将失败。 You should either make the service public, or stop using the container directly and use dependency injection instead. 您应该公开服务,或者直接停止使用容器,而改用依赖项注入。

Here is my controller: 这是我的控制器:

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

My services.yml: 我的services.yml:

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

I looked into the docs, it says that in Symfony3.4, services are private by default. 我查看了文档,它说在Symfony3.4中,默认情况下服务是私有的。 I am using autowiring in my app, so how I get the fos_user.mailer without any deprecation warnings? 我在我的应用程序中使用自动装配,那么如何获得fos_user.mailer而没有任何弃用警告?

I tried setting Fosuserbundle services to public, doesnt help: 我尝试将Fosuserbundle服务设置为公共服务,没有帮助:

services.yml : services.yml

    ....
    FOS\UserBundle:
            public: true

Any help appreciated! 任何帮助表示赞赏!

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

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

It's better to use DependencyInjection instead of call container directly. 最好使用DependencyInjection而不是直接调用容器。 You should pass your mailer to your method: 您应该将邮件传递给您的方法:

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

For more informations about dependencyInjection : https://symfony.com/doc/3.4/service_container/injection_types.html 有关DependencyInjection的更多信息: https : //symfony.com/doc/3.4/service_container/injection_types.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM