user2143356
user2143356

Reputation: 5607

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

I want to extend the DefaultAuthenticationFailureHandler in Symfony2.

I don't want to replace it, just extend it.

I basically want to hook in on the failure handler so if a condition is met instead of redirecting as the failure handler does, I can perform a different action.

I'm thinking I need to do this as a service.

I have this in services.yml

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

In security.yml:

security:

    firewalls:

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

In AuthenticationFailure.php

namespace Symfony\Component\Security\Http\Authentication;

class AuthenticationFailure extends DefaultAuthenticationFailureHandler
{

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {

        //do something here instead

    }

}

...but I get this error:

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

Upvotes: 1

Views: 1214

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52513

wrong namespace

namespace Symfony\Component\Security\Http\Authentication should be MyBundle\Bundle in your extending class.

missing use statements

make sure you have the use statements for Request and AuthenticationException in your extending class.

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

Otherwise php will look for non-existant and none-compatible classes MyBundle\Bundle\Request and MyBundle\Bundle\AuthenticationException.

Upvotes: 1

Related Questions