Twig“U Error”语法:函数;是“授予”吗;不存在

Twig“U Error”语法:函数;是“授予”吗;不存在,twig,silex,Twig,Silex,我使用的是Silex,无法在模板中使用is_grated函数。我在文档中找不到任何关于为什么这不起作用的信息。有什么提示吗 $app->register(new Silex\Provider\SecurityServiceProvider()); $app->register(new Silex\Provider\TwigServiceProvider(), array( 'twig.path' => __DIR__.'/../templates', 'twi

我使用的是Silex,无法在模板中使用is_grated函数。我在文档中找不到任何关于为什么这不起作用的信息。有什么提示吗

$app->register(new Silex\Provider\SecurityServiceProvider());

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/../templates',
    'twig.options' => array('cache' => __DIR__.'/../cache'),
));

$app['debug'] = true;

$app['security.firewalls'] = array(
    'login' => array(
                'pattern' => '^/login$',
        ),
        'secured' => array(
                'pattern' => '^.*$',
                'form' => array('login_path' => '/login', 'check_path' => '/login_check'),
                'users' => array(
                        'admin' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
                ),
        ),
);

$app->get('/', function() use ($app) {
    return $app['twig']->render('index.html.twig');
});

$app->get('/login', function(Request $request) use ($app) {
    return $app['twig']->render('login.html.twig', array(
            'error'                 => $app['security.last_error']($request),
            //'last_username' => $app['session']->get('_security.last_username'),
    ));
});

显然,我还需要添加symfony/bridge组件:

将其添加到composer.json并更新

"symfony/twig-bridge": "2.1.*",

嘿。。。它会像预期的那样工作。

我不得不使用这个解决方法(不知道是否有任何缺点)


根据
Symfony\Component\Security\Core\securitycontenterface
我们必须提供第二个参数,我们试图评估哪些权限。第二个参数将发送给投票人(如用户)


问题很可能是由注册类的顺序引起的。顺序应该是SecurityServiceProvider,然后启动应用程序,然后是TwigServiceProvider注册。TwigServiceProvider检查$app['security']以设置Twig SecurityExtension。所以秩序很重要

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

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

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

如果您使用Symfony 2.6组件,它将变成安全性.授权检查程序,而不是安全性,如下所示:

$function = new Twig_SimpleFunction('is_granted', function($role,$object = null) use ($app){
    return $app['security.authorization_checker']->isGranted($role,$object);
});
$app['twig']->addFunction($function);

虽然我找不到,但这应该是你给Silex添加树枝的方式。twig bridge将自动包含twig/twig.naaby,您是否找到了其他解决方案而不使用此解决方案?嗯,还没有,但我正在进行另一个Silex项目,我将尝试另一种方法。我会让你不断更新我最终使用了你的应用程序,但我的应用程序设置不同-如果你有兴趣,这里有一个关于我当前架构的建议。这似乎是一个很好的工作。你使用了我最喜欢的许可证!;)分叉。
// Security service
$app["security.firewalls"] = array();
$app->register(new Silex\Provider\SecurityServiceProvider());

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

// Twig service
$app->register(new Silex\Provider\TwigServiceProvider(), array(
  'twig.path' => sprintf("%s/../views", __DIR__),
));
$function = new Twig_SimpleFunction('is_granted', function($role,$object = null) use ($app){
    return $app['security.authorization_checker']->isGranted($role,$object);
});
$app['twig']->addFunction($function);