Search code examples
twigsilex

Twig_Error_Syntax: The function "is_granted" does not exist


I'm using Silex and can't use the is_granted function in a template. I can't find anything in the docs about why this isn't working. Any hints?

$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'),
    ));
});

Solution

  • Apparently, I needed to add the symfony/bridge components as well:

    Add this to composer.json and update.

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

    And hey... it'll work like expected.