[英]Symfony 3 Email confirmation on FOSUserBundle Profile Edit
我嘗試跟隨這個論壇,向個人簡介編輯fos用戶群發送電子郵件會議。 我創建文件
/src/AppBundle/EventListener.php
namespace AppBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ChangeProfileListener implements EventSubscriberInterface
{
private $mailer;
private $tokenGenerator;
private $router;
private $session;
private $tokenStorage;
public function __construct(
MailerInterface $mailer,
TokenGeneratorInterface $tokenGenerator,
UrlGeneratorInterface $router,
SessionInterface $session, TokenStorageInterface $tokenStorage
) {
$this->mailer = $mailer;
$this->tokenGenerator = $tokenGenerator;
$this->router = $router;
$this->session = $session;
$this->tokenStorage = $tokenStorage;
}
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::PROFILE_EDIT_INITIALIZE => 'onProfileEditInitialize',
FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEditSuccess',
);
}
public function onProfileEditInitialize(GetResponseUserEvent $event)
{
// required, because when Success's event is called, session already contains new email
$this->email = $event->getUser()->getEmail();
}
public function onProfileEditSuccess(FormEvent $event)
{
$user = $event->getForm()->getData();
if ($user->getEmail() !== $this->email)
{
// disable user
$user->setEnabled(false);
// send confirmation token to new email
$user->setConfirmationToken($this->tokenGenerator->generateToken());
$this->mailer->sendConfirmationEmailMessage($user);
// force user to log-out
$this->tokenStorage->setToken();
// redirect user to check email page
$this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());
$url = $this->router->generate('fos_user_registration_check_email');
$event->setResponse(new RedirectResponse($url));
}
}
}
服役后.yml
parameters:
#parameter_name: value
oc_user.email_change.listener.class: AppBundle\EventListener\ChangeProfileListener
services:
app.form.registration:
class: AppBundle\Form\RegistrationType
tags:
- { name: form.type, alias: app_user_registration }
app.form.profileedit:
class: AppBundle\Form\ProfileType
tags:
- { name: form.type, alias: app_profile_edit }
...
oc_user.email_change.listener:
class: %oc_user.email_change.listener.class%
arguments: ['@fos_user.mailer', '@fos_user.util.token_generator', '@router', '@session', '@security.token_storage']
tags:
- { name: kernel.event_subscriber }
但是我總是有這個錯誤
(1/1)AutowiringFailedException無法自動裝配服務“ AppBundle \\ EventListener \\ ChangeProfileListener”:方法“ __construct()”的參數“ $ mailer”引用接口“ FOS \\ UserBundle \\ Mailer \\ MailerInterface”,但不存在這樣的服務。 您可能應該將此接口作為以下現有服務之一的別名:“ fos_user.mailer.default”,“ fos_user.mailer.twig_swift”,“ fos_user.mailer.noop”。
我也重寫了表格,但是可以用
可以幫我??
我的配置文件
# app/config/config.yml
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
registration:
form:
type: AppBundle\Form\RegistrationType
# if you are using Symfony < 2.8 you should use the type name instead
# type: app_user_registration
confirmation:
enabled: true
profile:
form:
type: AppBundle\Form\ProfileType
fos_user.mailer:
alias: 'fos_user.mailer.default'
錯誤消息已經說了:
您可能應該將此接口作為以下現有服務之一的別名:“ fos_user.mailer.default”,“ fos_user.mailer.twig_swift”,“ fos_user.mailer.noop”。
該錯誤發生在您的配置中(標記為--> <--
):
oc_user.email_change.listener:
class: %oc_user.email_change.listener.class%
arguments: [--> '@fos_user.mailer', <-- '@fos_user.util.token_generator', '@router', '@session', '@security.token_storage']
tags:
- { name: kernel.event_subscriber }
您可能必須在FOS UserBundle中啟用通知功能:
# app/config/config.yml
fos_user:
# ...
registration:
confirmation:
enabled: true
如文檔中所述: https : //symfony.com/doc/current/bundles/FOSUserBundle/emails.html#registration-confirmation
如果這樣做沒有幫助,您可能要在錯誤消息中引用提到的一項服務,或者創建一個指向其中一項的別名,例如fos_user.mailer.default
:
fos_user.mailer:
alias: 'fos_user.mailer.default'
然后,您可以保持服務不變,每當您引用fos_user.mail
,它將使用別名中引用的服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.