Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silex SecurityServiceProvider throws 'Identifier "security.authentication_providers" is not defined.'

Tags:

php

symfony

silex

I can't figure out how to use SecurityServiceProvider in Silex. My configuration is:

$app['security.firewalls'] = array(
    'admin' => array(
        'pattern' => '^/_admin/.+',
        'form' => array('login_path' => '/_admin/', 'check_path' => '/_admin/login_check'),
        'logout' => array('logout_path' => '/_admin/logout'),
        'users' => array(
            'admin' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsR...'),
        ),
    ),
);
$app->register(new Silex\Provider\SecurityServiceProvider());

This just throws:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Identifier "security.authentication_providers" is not defined.'

According to the documentation in some cases when you want to access Security features outside of the handling of a request you have to call $app->boot(); but this isn't my situation.
If I call $app->boot(); before $app->register(...) it doesn't raise any exception but it probably doesn't boot at all because then in generating login form Twig throws:

Unable to generate a URL for the named route "_admin_login_check" as such route does not exist.

There's an issue a few months ago with probably the same problem but it's closed so I guess it should be fixed now

like image 633
martin Avatar asked Aug 02 '13 17:08

martin


2 Answers

You have to boot your application between the SecurityServiceProvider registration and the TwigServiceProvider registration :

// Security service
$app["security.firewalls"] = array();
$app->register(new Silex\Provider\SecurityServiceProvider());

// Boot your application
$app->boot();

// Twig service
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => sprintf("%s/../views", __DIR__),
));

This code above seems to fix your problem but you must at least add one authentication provider.

like image 180
KuiKui Avatar answered Oct 20 '22 00:10

KuiKui


I was getting the same exception when trying to register the SecurityServiceProvider before the TwigServiceProvider.

I just changed the registering order (Security after Twig) and everything started to work fine:

// Twig service

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => sprintf("%s/../views", __DIR__),
));

// Security service

$app["security.firewalls"] = array();
$app->register(new Silex\Provider\SecurityServiceProvider());
like image 33
agmangas Avatar answered Oct 19 '22 22:10

agmangas