我正在尝试创建一个链接,在Symfony 3中注册后向用户发送确认令牌。
但是,我收到如下的弃用消息:
用户不推荐使用:“fos_user.mailer”服务是私有的,从Symfony 3.2开始从容器中获取它并在4.0中失败。您应该将服务公之于众,或者直接停止使用容器并改为使用依赖注入。
这是我的控制器:
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服务设置为公共,没有帮助:
services.yml
:
....
FOS\UserBundle:
public: true
任何帮助赞赏!
最好直接使用DependencyInjection而不是call container。您应该将邮件传递给您的方法:
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
使用$mailer = $this->container->get('fos_user.mailer');
而不是$mailer = $this->get('fos_user.mailer');