2017-11-27 31 views
1

저는 도움을 요청하는 데 익숙하지 않지만,이 문제에 대해서는 스스로 해결할 수 없다고 고백합니다.symfony의 서비스를 통해 이전 서버를 보내는 방법

동일한 양식 (loginform 및 fosuserbundle의 등록 양식)에 2 개의 양식이 있습니다. 두 양식 모두 작동합니다.

기존 사용자가 가입하려고하면 내 문제가 발생합니다.

전자 메일이 이미있는 경우 동일한 페이지에서 사용자를 리디렉션하는 RegistrationFailureListener을 생성했습니다.

그러나이 양식에는 register/ 경로에 표시되는 오류가 표시되지 않습니다.

내 양식에 이러한 양식 오류를 보내는 방법을 모르겠습니다.

죄송합니다.

내 코드 :

<?php 
    namespace TNS\UserBundle\EventListener; 
    use FOS\UserBundle\Event\FormEvent; 
    use FOS\UserBundle\FOSUserEvents; 
    use FOS\UserBundle\Mailer\MailerInterface; 
    use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
    use Symfony\Component\HttpFoundation\RedirectResponse; 
    use Symfony\Component\HttpFoundation\Session\SessionInterface; 
    use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 
    use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface; 


    class RegistrationListener implements EventSubscriberInterface 
    { 

     /** 
     * @var UrlGeneratorInterface 
     */ 
     private $router; 
     /** 
     * @var TokenGeneratorInterface 
     */ 
     private $tokenGenerator; 
     /** 
     * @var SessionInterface 
     */ 
     private $session; 
     /** 
     * @var MailerInterface 
     */ 
     private $mailer; 


     /** 
     * RegistrationSuccessListener constructor. 
     * @param UrlGeneratorInterface $router 
     * @param TokenGeneratorInterface $tokenGenerator 
     * @param SessionInterface $session 
     * @param MailerInterface $mailer 
     */ 
     public function __construct(UrlGeneratorInterface $router, TokenGeneratorInterface $tokenGenerator, SessionInterface $session, MailerInterface $mailer) 
     { 
      $this->router = $router; 
      $this->tokenGenerator = $tokenGenerator; 
      $this->session = $session; 
      $this->mailer = $mailer; 
     } 

     /** 
     * Returns an array of event names this subscriber wants to listen to. 
     * 
     * The array keys are event names and the value can be: 
     * 
     * * The method name to call (priority defaults to 0) 
     * * An array composed of the method name to call and the priority 
     * * An array of arrays composed of the method names to call and respective 
     * priorities, or 0 if unset 
     * 
     * For instance: 
     * 
     * * array('eventName' => 'methodName') 
     * * array('eventName' => array('methodName', $priority)) 
     * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) 
     * 
     * @return array The event names to listen to 
     */ 
     public static function getSubscribedEvents() 
     { 
      return array(
       FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess', 
       FOSUserEvents::REGISTRATION_FAILURE => 'onRegistrationFailure' 
      ); 
     } 

     public function onRegistrationFailure(FormEvent $event) {   
      $url = $this->router->generate('tns_core_homepage'); 
      $event->setResponse(new RedirectResponse($url)); 

     } 
public function onRegistrationSuccess(FormEvent $event) 
    { 
     /** @var $user \FOS\UserBundle\Model\UserInterface */ 
     $user = $event->getForm()->getData(); 

     $user->setEnabled(false); 
     if (null === $user->getConfirmationToken()) { 
      $user->setConfirmationToken($this->tokenGenerator->generateToken()); 
     } 

     $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)); 

    } 
} 

내 RegisterController이

+0

이 대답은 여기에 리디렉션 대신 템플릿을 렌더링하기 위해 제안 ..... 다른 사람을 도울 수 있다면 - https://stackoverflow.com/questions/17269844/symfony-2-separate-form- logic-show-form-errors-after-redirect – Bananaapple

+0

괜찮 았어, 나는 내 서비스에 템플릿을 삽입하려고했는데 이제는 리다이렉트 대신 렌더링을한다. 하지만 이제는 커스텀 페이지 대신에 레지스터/경로로 방향이 바뀌었고 원하는 바가 아니 었습니다. 내 서비스에 templating을 주입하는 방법을 배웠습니다. –

+0

registercontroller에서 내 등록 작업이 끝날 때 렌더링을 변경합니까? –

답변

0

좋아들 fosuser의 registercontroller과 동일합니다! 나는 내 문제를 해결했다.

<?php 
namespace TNS\UserBundle\EventListener; 


use FOS\UserBundle\Event\FormEvent; 
use FOS\UserBundle\FOSUserEvents; 
use Symfony\Bundle\TwigBundle\TwigEngine; 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\HttpFoundation\Response; 

class RegistrationFailureListener implements EventSubscriberInterface 
{ 
    /** 
    * @var EngineInterface 
    */ 
    private $templating; 


    public function __construct(TwigEngine $templating) { 
     $this->templating = $templating; 
    } 

    /** 
    * Returns an array of event names this subscriber wants to listen to. 
    * 
    * The array keys are event names and the value can be: 
    * 
    * * The method name to call (priority defaults to 0) 
    * * An array composed of the method name to call and the priority 
    * * An array of arrays composed of the method names to call and respective 
    * priorities, or 0 if unset 
    * 
    * For instance: 
    * 
    * * array('eventName' => 'methodName') 
    * * array('eventName' => array('methodName', $priority)) 
    * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) 
    * 
    * @return array The event names to listen to 
    */ 
    public static function getSubscribedEvents() { 
     return array(
      FOSUserEvents::REGISTRATION_FAILURE => 'onRegistrationFailure' 
     ); 
    } 

    public function onRegistrationFailure(FormEvent $event) { 
     $form = $event->getForm(); 
     $response = $this->templating->render('TNSUserBundle:Security:login.html.twig', array(
      'form'=>$form->createView() 
     )); 
     $event->setResponse(new Response($response)); 

    } 

나는이처럼 내보기를 변경 : 양식 var에 정의 된 경우

{% trans_default_domain 'FOSUserBundle' %} 
    {% if error is defined and error is not null%} 
     <div class="alert alert-danger col col-md-12">{{ error.messageKey|trans(error.messageData, 'security') }}</div> 
    {% endif %} 
    <div class="p-0 card col col-md-5 col-12"> 
      <h3 class="p-2 bg-primary text-white">Se connecter</h3> 
      <form action="{{ path("fos_user_security_check") }}" method="post" class="p-2"> 
       {% if csrf_token is defined%} 
        <input type="hidden" name="_csrf_token" value="{{ csrf_token }}"/> 
       {% endif %} 
       <div class="form-group"> 
        <label for="username">{{ 'security.login.username'|trans }}</label> 
        <input type="text" id="username" class="form-control" name="_username" value="{% if last_username is defined %}{{ last_username }}{% endif %}" 
          required="required"/> 
       </div> 
       <div class="form-group"> 
        <label for="password">{{ 'security.login.password'|trans }}</label> 
        <input type="password" id="password" class="form-control" name="_password" required="required"/> 
       </div> 
       <div class="form-check"> 
        <label class="form-check-label"> 
         <input type="checkbox" id="remember_me" class="form-check-input" name="_remember_me" value="on"/> 
         {{ 'security.login.remember_me'|trans }} 
        </label> 
       </div> 
       <div class="form-group" align="center" style="margin-top: 54%"> 
       <input type="submit" class="btn btn-primary" id="_submit" name="_submit" 
         value="{{ 'security.login.submit'|trans }}"/> 
       </div> 
      </form> 
    </div> 
{% if form is defined %} 
    <div class="p-0 card col col-md-5 col-12"> 
     <h3 class="p-2 bg-primary text-white">S'inscrire</h3> 
     {{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register p-2'}}) }} 
     {{ form_widget(form) }} 
     {{ form_errors(form) }} 
     {{ form_widget(form._token) }} 
     <div align="center"> 
      <input type="submit" class="btn btn-primary" value="{{ 'registration.submit'|trans }}" /> 
     </div> 
     {{ form_end(form) }} 
    </div> 
{% else %} 
    <div class="p-0 card col col-md-5 col-12"> 
     <h3 class="p-2 bg-primary text-white">S'inscrire</h3> 
     {{ render(controller('TNSUserBundle:Registration:register')) }} 
    </div> 
{% endif %} 

마지막으로 내보기에 내가 그래 난 다른 양식을 표시하면 내가 전화 테스트 나는 새로운 가입자를 생성 RegisterController의 양식

올바른 방법인지는 모르겠지만 양식 오류가 표시되고 올바른 페이지로 리디렉션됩니다.