[ How to mix content for logged users and not logged users in Symfony 2.3 or superior ]
What is the best approach to do this in Symfony 2.3 or superior:
I have a web page that can be posted in a public area or in a secured area.
When the user open the secured web page version, lets say /SecureArea/page1 is able only to see the content and do more thing but when a user is not logged in, they can see the same content in /PublicArea/page1 but can only see the content not doing anything else at least of course they login.
Securing a web page in Symfony is quite easy , you put your web page inside a secure area, but what I am trying to do is to have the same version of the webpage in both, users are able to see the same content but can only do more things if they are logged into the system. I don't want to repeat myself and create two different versions of the same page.
Answer 1
For this case you can make decision what to do based on is_granted('IS_AUTHENTICATED_FULLY')
in twig, which basically checks if current user is logged in, so example would be like:
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
{# do something that only logged in users could see #}
{% else %}
{# do something that unauthenticated users can see #}
{% endif %}
If you want to check if user is logged in or not in controller instead:
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
// user logged in
} else {
// not logged in
}