Symfony2-试图设置一个服务来扩展一个类


Symfony2 - trying to set up a service to extend a class

我想扩展Symfony2中的DefaultAuthenticationFailureHandler。

我不想替换它,只是扩展它。

我基本上想挂接故障处理程序,这样如果满足条件,而不是像故障处理程序那样重定向,我就可以执行不同的操作。

我想我需要做这项服务。

我有这个服务。yml

extend.auth.fail:
    class: MyBundle'Bundle'AuthenticationFailure

在安全方面。yml:

security:
    firewalls:
        secure_area:
            pattern: ^/admin
            form_login:
                login_path: login
                check_path: login_check
                failure_handler: extend.auth.fail

在AuthenticationFailure.php 中

namespace Symfony'Component'Security'Http'Authentication;
class AuthenticationFailure extends DefaultAuthenticationFailureHandler
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        //do something here instead
    }
}

但我得到了这个错误:

FatalErrorException: Compile Error: Declaration of Symfony'Component'Security'Http'Authentication'AuthenticationFailure::onAuthenticationFailure() must be compatible with that of Symfony'Component'Security'Http'Authentication'AuthenticationFailureHandlerInterface::onAuthenticationFailure() in /var/symfony/src/MyBundle/Bundle/AuthenticationFailure.php line 18

错误的命名空间

命名空间Symfony'Component'Security'Http'Authentication应该是扩展类中的MyBundle'Bundle

缺少use语句

确保在扩展类中有Request和AuthenticationException的use语句。

use Symfony'Component'HttpFoundation'Request;
use Symfony'Component'Security'Core'Exception'AuthenticationException;

否则php将查找不存在且不兼容的类MyBundle'Bundle'RequestMyBundle'Bundle'AuthenticationException