SonataAdminBundle of Symfony2

SonataAdminBundle of Symfony2

What is Sonata Admin Bundle?
SonataAdminBundle helps you manage your data using a graphic interface that will let you create, update or search your model’s instances. It is the second most essential bundle of Symfony2 development.

First thing first. Let’s create a project for our tutorial in sadmin folder.

$composer create-project symfony/framework-standard-edition sadmin/

Now tell composer to install sonata admin bundle—

$ composer require sonata-project/admin-bundle

We need to install one more bundle for our tutorial –

$ composer require sonata-project/doctrine-orm-admin-bundle

Add the following bundle information in our app/AppKernel.php

// Add your dependencies
        new Sonata\CoreBundle\SonataCoreBundle(),
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),

        // If you haven't already, add the storage bundle
        // This example uses SonataDoctrineORMAdmin but
        // it works the same with the alternatives
        new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),

        // Then add SonataAdminBundle
        new Sonata\AdminBundle\SonataAdminBundle(),

Add the configuration to app/config/config.yml

default_contexts: [cms]
# Enable the SonataAdminBundle block
contexts: [admin]
# Your other blocks

Now, install the assets from the bundles:

php app/console assets:install web

Add your routing in app/config/routing.yml

resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
prefix: /admin

resource: .
type: sonata_admin
prefix: /admin

Now go to –


You should see –

Sonata deshboard

If you see any error clear your cash and log by following command –

$ rm -rf app/cache/*
$ rm -rf app/logs/*

Let’s create a CMS bundle.

$ php app/console generate:bundle --namespace=Custom/CMSBundle

Create an Admin class as src/Custom/CMSBundle/Admin/PostAdmin.php


namespace Custom\CMSBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class PostAdmin extends Admin
    protected function configureFormFields(FormMapper $formMapper)
            ->add('title', 'text', array('label' => 'Post Title'))
            ->add('author', 'text', array('label' => 'Author'))
            ->add('slug', 'text', array('label' => 'Slug'))

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)

    protected function configureListFields(ListMapper $listMapper)

Now configure Custom/CMSBundle/Resources/config/admin.yml

class: Custom\CMSBundle\Admin\PostAdmin
- { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" }
- ~
- Custom\CMSBundle\Entity\Post
- ~
- [ setTranslationDomain, [CustomCMSBundle]]

Now configure app/config/config.yml

- { resource: @CustomCMSBundle/Resources/config/admin.yml }

Load this bundle in Custom/CMSBundle/DependencyInjection/CustomCMSExtension.php

public function load(array $configs, ContainerBuilder $container)

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

And don’t forget to uncomment app/config/config.yml

translator: { fallbacks: ["%locale%"] }

Now go to your dash board again –


admin bundle with content block

We are done with our admin panel.

To change the upper left corner icon and title –

$ php app/console assets:install

And add the title and logo in app/config/config.yml

title: Custom CMS Bundle
title_logo: bundles/customcms/images/tiger5043.png

Now let’s create a post entity for our blog from command prompt.

The entity will have following four fields –
1. title
2. author
3. body
4. slug

Let’s do it –

$ php app/console doctrine:generate:entity

Now update your schema so that the table is created –

$ php app/console doctrine:schema:update --force

Now see the magic.

Click on Add New link in your dashboard –

CRUD with SonataAdminBundle

Can you remember we have not created any controller and view for our application? We have only created entity. Rest of post operations are handled by SonataAdminBundle.

Source code available in git — Download Source Code

Spend some time with this admin panel to check the magic of it.

Happy coding … 🙂

SonataAdminBundle of Symfony2

One comment to “SonataAdminBundle of Symfony2”

Leave a Reply

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