2011-12-28 2 views
5

가능한 중복 : 여기 제안 내가 AJAX 로그인 요청을 처리하기 위해 사용자 정의 인증 처리기 서비스를 구현 한
Symfony2 AJAX Login콜링 기본 핸들러

: https://stackoverflow.com/a/8312188/267705

하지만 정상 로그인 요청을 처리하려면 어떻게해야합니까? 기본 동작을 호출하는 것이 좋겠지 만 잘 모르지만 수행 방법을 찾지 못했습니다.

+0

정확하게 * handle *을 (를) 의미합니까? –

+0

질문에있는 링크를 보면, 나는 void else 문을 의미합니다. –

+0

글쎄, 나는 당신이 연결하고있는 답변을 썼다. ;) 내가 이해하지 못하는 것은 * handle *이라는 단어가 의미하는 바입니다. 기술적 인 세부 사항이없는 사용 사례는 무엇입니까? –

답변

6

이 당신이 원하는 것을 달성하기위한 방법 중 하나입니다 :

namespace YourVendor\UserBundle\Handler; 

// "use" statements here 

class AuthenticationHandler 
implements AuthenticationSuccessHandlerInterface, 
      AuthenticationFailureHandlerInterface 
{ 
    private $router; 

    public function __construct(Router $router) 
    { 
     $this->router = $router; 
    } 

    public function onAuthenticationSuccess(Request $request, TokenInterface $token) 
    { 
     if ($request->isXmlHttpRequest()) { 
      // Handle XHR here 
     } else { 
      // If the user tried to access a protected resource and was forces to login 
      // redirect him back to that resource 
      if ($targetPath = $request->getSession()->get('_security.target_path')) { 
       $url = $targetPath; 
      } else { 
       // Otherwise, redirect him to wherever you want 
       $url = $this->router->generate('user_view', array(
        'nickname' => $token->getUser()->getNickname() 
       )); 
      } 

      return new RedirectResponse($url); 
     } 
    } 

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception) 
    { 
     if ($request->isXmlHttpRequest()) { 
      // Handle XHR here 
     } else { 
      // Create a flash message with the authentication error message 
      $request->getSession()->setFlash('error', $exception->getMessage()); 
      $url = $this->router->generate('user_login'); 

      return new RedirectResponse($url); 
     } 
    } 
} 

을 즐길 수 있습니다. ;)

+0

인증 실패시 'user_login'경로를 생성 할 때 오류가 있습니다. 무엇을 시도해도 (login, login_path, user_login) 경로 생성에 실패하고 "Route does not exist"오류가 발생합니다. 이 경로는 어디에 구성되어 있습니까? 생성을 위해 수정하려면 어떻게해야합니까? –

+0

@ 가브리엘 테론 (GabrielTheron), 평소 행동에 대해 정의한 일반적인 경로 일뿐입니다. [traditinal 로그인 양식에 대한 섹션] (http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form)을 읽으십시오. –

+0

답장을 보내 주셔서 감사합니다. FOSUserBundle을 사용하고 있으며,이 경로 대신 'fos_user_security_login'을 사용하는 것이 좋습니다. 그것은 나를 위해 일한 유일한 길입니다. –