I'm new to symfony and twig and I have som开发者_如何学Goe headache with security, firewalls and templates.
What I'm trying to do is to have a "base" template that shows a topbar. I would like this top bar show a "You are not logged in" if the user is not logged and a "Welcome user U" message if the user is logged. Because this I put an
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
in the "base" template to differentiate between logged and anonymous users but I have problems about security context tokens.
My public paths (not secured by firewall) are:
/myapp/
/myapp/home
/myapp/about
/myapp/help
and later there are some paths for actions only can access authenticated users:
/myapp/action1
/myapp/action2
...
/myapp/actionN
The problem is, once a user is logged in my "base" show the welcome message in the view of actions1, ..., actionN but when user goed to "home" or "help" pages the message is "you are not logged in".
Some has a similar situacion? how did you solve it? how are your router and security files configured?
The firewall doesn't share the security context. So when a action is not behind the firewall you can't acces the user info. Try placing the entire app behind the firewall (and allow anonymous users):
firewalls:
secured_area:
pattern: ^/
anonymous: ~
form_login:
check_path: /login_check
login_path: /login
logout:
path: /logout
target: /
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/action, roles: ROLE_USER }
Make sure the login_path can be accessed by anonymous users.
精彩评论