2012-01-29 2 views
21

Для успешного входа в систему есть параметр use_referer: true. Для входа в систему существует только failure_path, что не то, что я ищу.Как вернуться к рефереру после неудачи входа?

Вторая вещь: Как это сделать и передать сообщение об ошибке?

Третья вещь: Как вернуться к рефереру после выхода из системы?

+0

Почему бы не путь сбоя просто приглашение для входа? – JamesHalsall

+1

, потому что я могу войти с каждой подстраницы. У меня нет одной формы для регистрации на указанном URL-адресе, поэтому я не хочу перенаправлять пользователя на главную страницу, когда он заходит с другой страницы профиля пользователя. –

ответ

49

Я решил.

Существует решение: How to disable redirection after login_check in Symfony 2

и вот код, который решает мою проблему:

<?php 

namespace Acme\MainBundle\Handler; 

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface; 
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; 
use Symfony\Component\Security\Core\Exception\AuthenticationException; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RedirectResponse; 

class AuthenticationHandler implements AuthenticationFailureHandlerInterface, LogoutSuccessHandlerInterface 
{ 
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception) 
    {  
     $referer = $request->headers->get('referer');  
     $request->getSession()->setFlash('error', $exception->getMessage()); 

     return new RedirectResponse($referer); 
    } 

    public function onLogoutSuccess(Request $request) 
    { 
     $referer = $request->headers->get('referer'); 
     return new RedirectResponse($referer); 
    } 
} 

для обработки событий добавить в security.yml, например:

form_login: 
    check_path: /access/login_check 
    login_path:/
    use_referer: true 
    failure_handler: authentication_handler 
logout: 
    path: /access/logout 
    target:/
    success_handler: authentication_handler 

и CONFIG .yml:

services: 
    authentication_handler: 
     class: Acme\MainBundle\Handler\AuthenticationHandler 
+4

$ request-> headers-> get ('referer'); иногда возвращает null (я воспроизвел в Firefox). Это решение не является надежным, требуется резервное или другое решение. – Julien

+0

@ wojciech-kulik У меня есть следующая ошибка: '' '$ request-> getSession() -> setFlash ('error', $ exception-> getMessage());' '' '' 'FatalErrorException: Ошибка: Вызов неопределенного метода Symfony \ Component \ HttpFoundation \ Session \ Session :: setFlash() в /var/www/symfony/src/Application/Sonata/UserBundle/Form/Handler/AuthenticationHandler.php line 16''' – jcarlosweb

+0

@webyseo Вероятно, у вас есть другая версия Symphony, а флеш-сообщения настроены по-другому. Возможно, это поможет: http://stackoverflow.com/questions/13348534/symfony-2-setting-a-flash-message-outside-of-controller –

Смежные вопросы