When having a backend for admin users, it is interesting to have a login form, and at the same time having a normal login form for normal users in the public area of our website.
Is that possible using FOSUserBundle? How can it be done "the Symfony2" way?
First we need to configure some special routes for the admin area:
Next configure a special firewall for the admin area using these routes, and define them to be anonymously accessed:
Ok! We have just separated our login system in two parts: admin and main.
Let's override the SecurityController. For that we will need to create a custom bundle which parent is FOSUserBundle (check the doc for that). In this new bundle, create the controller:
That's it! Now you can write your AdminBundle:Security:login.html.twig :)
NOTE: Don't forget to use the admin routes in your admin area! (in the login form action, the logout link, etc)
Regarding to the approved answer, I made some adjustments in my Symfony 3.2.8 project in order to work correctly.
Instead of
$requestAttributes = $this->container->get('request')->attributes;
in the Security Controller, I used$requestAttributes = $this->container->get('request_stack')->getCurrentRequest();
.