我的重定向適用於註銷,但不適用於登錄,它保留在登錄頁面但是引用程序具有適當的值(我在登錄表單中顯示{{ app.request.headers.get('referer') }}
)。Symfony2:登錄成功後重定向到referer(FOSUserBundle)
Security.yml:
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: security.csrf.token_manager
use_referer: true
success_handler: authentication_handler
logout:
success_handler: authentication_handler
anonymous: true
處理器:
namespace LeJardinEbene\Bundle\Form\Handler;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, LogoutSuccessHandlerInterface
{
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$referer = $request->headers->get('referer');
return new RedirectResponse($referer);
}
public function onLogoutSuccess(Request $request)
{
$referer = $request->headers->get('referer');
return new RedirectResponse($referer);
}
}
Services.yml:
services:
authentication_handler:
class: LeJardinEbene\Bundle\Form\Handler\AuthenticationHandler
是否有人KN這個問題是什麼?
UPDATE
正如你勸我我分裂處理程序在2:
class LoginAuthenticationHandler
{
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
//$referer = $request->headers->get('referer');
$referer = $event->getRequest()->headers->get('referer');
return new RedirectResponse($referer);
}
}
class LogoutAuthenticationHandler implements LogoutSuccessHandlerInterface
{
public function onLogoutSuccess(Request $request)
{
$referer = $request->headers->get('referer');
return new RedirectResponse($referer);
}
}
services:
login_authentication_handler:
class: LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }
logout_authentication_handler:
class: LeJardinEbene\Bundle\Form\Handler\LogoutAuthenticationHandler
但現在我得到以下錯誤:
Fatal error: Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Catchable Fatal Error: Argument 1 passed to Symfony\Component\Security\Http\Authentication\CustomAuthenticationSuccessHandler::__construct() must implement interface Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface, instance of LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler given
更新2
namespace LeJardinEbene\Bundle\Form\Handler;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
class LoginAuthenticationHandler extends AuthenticationSuccessHandlerInterface
{
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$referer = $event->getRequest()->headers->get('referer');
return new RedirectResponse($referer);
}
}
產生錯誤:
Fatal error: Class LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler cannot extend from interface Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface in /Applications/XAMPP/xamppfiles/htdocs/symfony2/src/LeJardinEbene/Bundle/Form/Handler/LoginAuthenticationHandler.php on line 22
更新3
我已經嘗試過,它會導致這個錯誤:
Fatal error: Class LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface::onAuthenticationSuccess) in /Applications/XAMPP/xamppfiles/htdocs/symfony2/src/LeJardinEbene/Bundle/Form/Handler/LoginAuthenticationHandler.php on line 22
,如果我這樣做一切都改變了沒有?我的意思是,沒有更多的$事件等,但$請求和$令牌再次作爲參數...這不起作用,當我打開這個話題,arghhhhhhhhhhh!
UPDATE 4
services.yml
services:
login_handler:
class: LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }
security.yml
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: security.csrf.token_manager
use_referer: true
success_handler: login_handler
logout:
success_handler: logout_authentication_handler
anonymous: true
沒有錯誤了,但不回去引用者...也不會是因爲我的security.yml的?也許它不是「success_handler:login_authentication_handler」。看起來令人難以置信,它仍然無法正常工作。無論如何非常感謝您的幫助 –
Waw ...肯定是一個錯誤,嘗試在'services.yml'中和在'success_handler'的安全性中更改服務的名稱。 對不起! – chalasr
嘗試但相同,請參閱更新4 ;-) –