I use Silex and the SecurityProvider, my firewall :
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'user' => array(
'pattern' => '^/user/',
'form' => array(
'login_path' => '/connexion',
'check_path' => '/user/login_check',
'default_target_path' => 'homepage_user'
),
'logout' => array('logout_path' => '/user/deconnexion')
...
)
)
));
It works ! But I didn't find any way to access to the user object in the template, the symfony synthax doesn't work :
{{ app.user }}
So I add a new global in Twig like this :
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$token = $app['security']->getToken();
$user = ($token === null) ? null : $token->getUser();
$twig->addGlobal('user', $user);
return $twig;
}));
It works but not outside the secured area: $token is null
My question is simple : How can I access to the user outside of the secured area ?
Thank you
EDIT: I tried to add a firewall with anonymous = true, like this :
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'user' => array(
'pattern' => '^/user/',
'form' => array(
'login_path' => '/connexion',
'check_path' => '/user/login_check',
'default_target_path' => 'homepage_user'
),
'logout' => array('logout_path' => '/user/deconnexion'),
...
),
'unsecured' => array(
'anonymous' => true
)
)
));
But it doesn't work, outside of the secured area, when the user is logged, the token is "anon."