私はajaxとFOSUserBundle
symfony2で認証を実装しています。これは security.yml の私のコードです:
main:
pattern: ^/
form_login:
provider: fos_userbundle
default_target_path: hello
remember_me: true
success_handler: my.success_handler
failure_handler: my.success_handler
check_path: /login_check
login_path: /login
remember_me:
key: %secret%
anonymous: false
logout: true
そしてservices.yml:
my.success_handler:
class: Kgr\UserBundle\Controller\SuccessHandler
arguments: [@router]
およびクラス SuccessHandler :
namespace Kgr\UserBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
class SuccessHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
protected $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if($request->isXmlHttpRequest()){
$response = new Response(json_encode(array(
'success'=> 1,
)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}else{
if ($token->getUser()->isSuperAdmin()) {
return new RedirectResponse($this->router->generate('admin'));
}
else {
return new RedirectResponse($this->router->generate('MyAppmondeUserBundle_homepage'));
}
}
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if($request->isXmlHttpRequest()){
$response = new Response(json_encode(array(
'success'=> 0,
)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}else{
if ($token->getUser()->isSuperAdmin()) {
return new RedirectResponse($this->router->generate('admin'));
}
else {
return new RedirectResponse($this->router->generate('MyAppmondeUserBundle_homepage'));
}
}
}
}
エラーがわかりませんでした:
Fatal error: Declaration of Kgr\UserBundle\Controller\SuccessHandler::onAuthenticationFailure() must be compatible with that of Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface::onAuthenticationFailure() in D:\wamp1\www\Symfony\src\Kgr\UserBundle\Controller\SuccessHandler.php on line 12
単独で歩く onAuthenticationSuccess によるメソッドに対して