How to use the Symfony2 SonataAdminBundle

I have been doing a lot of Symfony2 development lately for our project ProTalk and one of the things we needed was a backend for our database. This should be a fairly simple backend to start with, just an easy way to get data into the database. So I thought I would use the SonataAdminBundle to easy generate this backend based on the doctrine2 entities that we already have. Eventually, I got it working, but it took me some time to find out exactly how, so I thought I’d share my experiences.First of all, after trying some things by hand, I found this great blog post to help me get the bundle installed with login functionality as well. This helped me a lot since the actual documentation on the bundle was not clear to me. I found the overall documentation on this bundle bad. It assumes certain knowledge and terminology that someone with little experience in Symfony2 doesn’t have.

After getting the bundles correctly installed, I got on to the next step which was actually creating my first menu item in the backend. I started out with something simple, so I took a table called tag, which only contains an id and a name field, as an example. The documentation stated I should add some code to the config.yml file (which I didn’t have before but created during the installation). So I added the following code to the config.yml file:

        class: Protalk\AdminBundle\Admin\TagAdmin
        - { name: sonata.admin, manager_type: orm, group: protalk_common, label: Tag} 
        arguments: [null, Protalk\MediaBundle\Entity\Tag, ProtalkAdminBundle:TagAdmin]

Make sure you also add the group (in this case protalk_common) to the section: sonata_admin / dashboard / groups in the config.yml file. This can be used to make menu’s and submenu’s. After this, I needed to create the admin class as shown in the code above, this is placed in the Protalk/AdminBundle/Admin directory. I created a special AdminBundle for the backend, but this is not necessary at all, you could also use an existing bundle as well. The code for my Admin class looks like this:

namespace Protalk\AdminBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
class TagAdmin extends Admin
    protected function configureFormFields(FormMapper $formMapper)
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    protected function configureListFields(ListMapper $listMapper)
    public function validate(ErrorElement $errorElement, $object)
        $errorElement->with('name')->assertMaxLength(array('limit' => 50))->end();

Now, according to the documentation I found online, this should be it and it should work now. But they are wrong, because we also need a controller to be able to get it working. These controllers are in fact empty controllers, since the SonataAdminBundle will take care of all the actions for us. I created the TagController.php in the Protalk/AdminBundle/Controller directory and it contains this code:

namespace Protalk\AdminBundle\Controller;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
class TagAdminController extends Controller

That is it really. For me, the documentation made no sense until I learned (thanks to my friend Stefan Koopmanschap) that I needed the controller as well. Finally it all works and I am ready to do more advanced stuff with the SonataAdminBundle.

This entry was posted in PHP and tagged , , , . Bookmark the permalink.

11 Responses to How to use the Symfony2 SonataAdminBundle

  1. Goran says:

    Actually, you don’t need controller… I have a lot of working admins without a single controller.

    • lineke says:

      This was for me the only way to get it working, but I am interested in how you did it. Could you share some code please?

      • Stof says:

        Simply pass null as third argument instead of passing the name of your controller.
        If you specify your own controller, it indeed need to exist but the default one can be used for many cases.

        • Roy says:

          What third argument?

          • lineke says:

            The third argument in this piece of the config.yml configuration:
            arguments: [null, Protalk\MediaBundle\Entity\Tag, ProtalkAdminBundle:TagAdmin]

            You can replace ProtalkAdminBundle:TagAdmin with null and then you don’t need a controller class.

  2. Thomas R. says:

    Thanks for sharing your issues with the SonataAdminBundle. As the project is open source, you can contribute to the documentation to make it more adequate with new comers.

    You can also post your question on the sonata mailing list.

    Thanks. Thomas

  3. cordoval says:

    if you are newcomer dont start with SAB, it is sometimes the worst start one can have. Fully recommend to do your own CRUD and customize it.

    • john says:

      Actually, until you are VERY experienced with Symfony2, stay far away from SonataAdmin.

      Although there is plenty of documentation, it is very difficult to understand to the point of wasting time even reading it.

      The simple things are fairly clear, but as soon as you need to do anything else, be prepared to dive into the code, as the documentation will not help you.

      Also, you find yourself more figuring out how to interact with Sonata Admin, than you do with Symfony.

      If you want to start off with something simpler and more easily extendable, try CedricLombardot’s AdmingeneratorBundle.

      Much, much easier to work with, and does just about everything you need.

      Having said all that, Sonata has more features and if you can figure out his documentation and code, you can probably get more out of it.

      For me though, it was a big waste of time in the beginning.

  4. Mareike says:

    Hi Lineke,

    You write “This can be used to make menu’s and submenu’s.” But how exactly can I implement a sub-menu with the groups parameter in the config.yml? Could you give me an example?

    Thanks, Mareike

  5. Mareike H. says:

    Hi Lineke,

    Thank you for your posting. You write, “This can be used to make menu’s and submenu’s.” but I have no idea how to build a submenu with the groups option. Can you give me an example?

    Thanks. Mareike

  6. TaylorJuicy says:

    I often visit your site and have noticed that you don’t update it often. More frequent updates will give your page higher authority & rank in google.
    I know that writing articles takes a lot of time, but you can always help yourself with miftolo’s tools
    which will shorten the time of creating an article to a few seconds.

Leave a Reply

Your email address will not be published. Required fields are marked *