Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

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

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 SilexProviderSecurityServiceProvider());

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 SilexProviderTwigServiceProvider(), array(
    'twig.path' => sprintf("%s/../views", __DIR__),
));

// Security service

$app["security.firewalls"] = array();
$app->register(new SilexProviderSecurityServiceProvider());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...