Symfony Guard with Sonata Admin

admin / 1 year / No Comments

Since Symfony 2.8 we have a nice and shiny Guard authentication component. Guard can replace SonataUserBundle and in the rest of this post, we will show you how. As there is great documentation about Guard setup, we will skip that part and go straight to making it work with Sonata Admin. We presume you already have User Entity, Authenticator and User Provider.

This post was created after my talk about Symfony Guard, be sure to check that out here.

First of all, we need a simple login form:

And a nice template to go with it (Thanks to SonataUserBundle this was an easy one):

We also need a Controller that will handle login and logout, logout function is empty because Symfony will handle that part but we still need to have it:

At the end, the only and the most important thing left is the firewall:

And we are done! Go to your login route and try your great new Authentication.