Silex SecurityServiceProvider throws 'Identifier “security.authentication_providers” is not defined.'

自闭症网瘾萝莉.ら 提交于 2019-11-27 18:22:24

问题


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


回答1:


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



回答2:


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.




回答3:


I ran into the same issue - also with the current silex version ~2.7.

Finally I found out, that in my case the "symfony/twig-bridge" component integrated via composer was the problem. I integrated that twig-bridge component to use the trans trait in my twig templates for translation (e.g. {{ 'Age'|trans }}). After removing twig-bridge from the project all worked as expected.

In order to use trans in my templates I have implemented an I18nExtension for myself to still use the trait syntax:

<?php 

namespace AppBundle\Utils;

class I18nExtension extends \Twig_Extension {
    private $app;

    /**
     * Register the extension after registering the TwigServiceProvider by
     * $app['twig']->addExtension(new AppBundle\Utils\I18nExtension($app));
     */
    public function __construct(\Silex\Application $app) {
        $this->app = $app;
    }

    /**
     * Provide an additional simple filter called trans - calling 
     * the translate function specified below.
     */
    public function getFilters() {
        return array(
            new \Twig_SimpleFilter('trans', array($this, 'translate')),
        );
    }

    /**
     * Translates the given $value using the translator registered in the app.
     */
    public function translate($value) {
        return $this->app['translator']->trans($value);
    }

    /**
     * Name of the extension.
     */
    public function getName() {
        return "I18nExtension";
    }
}


来源:https://stackoverflow.com/questions/18022890/silex-securityserviceprovider-throws-identifier-security-authentication-provid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!