2017-08-03 75 views
0

我嘗試按照此論壇給電子郵件confermation配置文件編輯fos用戶包。 我創建文件Symfony 3電子郵件確認FOSUserBundle簡介編輯

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

在service.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 \事件監聽\ 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' 

回答

1

錯誤消息laready說它:

你也許應該別名這個接口將這些現有的服務之一: 「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 

docs: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它將使用別名引用的服務。

+0

謝謝你的幫助,但我不確定明白..我已經啓用了註冊.....我嘗試把:fos_user.mailer: 別名:'fos_user.mailer.default'在我的配置文件??? –

+0

您是否按照以下說明操作:https://symfony.com/doc/current/bundles/FOSUserBundle/emails.html#registration-confirmation 這應該足夠了。 – dbrumann