9

我无法弄清楚如何SecurityServiceProviderSilex. 我的配置是:

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

这只是抛出:

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

根据文档,在某些情况下,当您想在处理必须调用的请求之外访问安全功能时,$app->boot();但这不是我的情况。
如果我在它没有引发任何异常$app->boot();之前调用$app->register(...)它,但它可能根本无法启动,因为在生成登录表单时 Twig 会抛出:

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

几个月前有一个问题可能是同样的问题,但它已经关闭,所以我想现在应该修复它

4

3 回答 3

12

尝试SecurityServiceProviderTwigServiceProvider.

我刚刚更改了注册顺序(Twig之后的Security ),一切都开始正常工作:

// 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());
于 2013-12-23T18:07:18.537 回答
12

SecurityServiceProvider您必须在注册和注册之间启动您的应用程序TwigServiceProvider

// 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__),
));

上面的代码似乎可以解决您的问题,但您必须至少添加一个身份验证提供程序。

于 2014-01-04T18:51:09.700 回答
0

我遇到了同样的问题 - 当前的 silex 版本 ~2.7 也是如此。

最后我发现,就我而言,通过作曲家集成的“symfony/twig-bridge”组件是问题所在。我集成了 twig-bridge 组件以在我的 twig 模板中使用trans trait 进行翻译(例如{{ 'Age'|trans }})。从项目中删除 twig-bridge 后,一切都按预期工作。

为了在我的模板中使用 trans 我已经为自己实现了一个 I18nExtension 以仍然使用特征语法:

<?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";
    }
}
于 2015-08-18T13:19:43.367 回答