Setup CRUD Admin using Symfony2 and SonataAdminBundle

Installing SonataAdminBundle and Configure

A complete guide to installing SonataAdminBundle via composer and configure bundle into Symfony project.


Today we will learn how to setup SonataAdminBundle into a Symfony2 project. SonataAdminBundle allows us to create a complete backend for our site. With SonataAdminBundle we can easily setup backend by typing some commands in command prompt. SonataAdminBundle can be installed at any moment during a project’s lifecycle, whether it’s a clean Symfony2 installation or an existing project. So, Now let’s start with installing Sonata Bundle into our Symfony project.

Download the Admin Bundle

To download the Sonata Admin Bundle we use below command that will be executed through composer. So to use this command you need to install composer first.

$ composer require sonata-project/admin-bundle 2.4.*@dev

Once you run this command on command prompt it downloads all dependent bundles:

- knplabs/knp-menu 
- knplabs/knp-menu-bundle 
- cocur/slugify 
- sonata-project/core-bundle 
- sonata-project/cache 
- sonata-project/block-bundle 
- sonata-project/exporter 
- twig/extensions
- sonata-project/admin-bundle

Enable downloaded Bundle

When all Admin Bundle has been downloaded then you can enable the bundle by adding the following line into app/AppKernel.php file.

<!--?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new AppBundle\AppBundle(),
        );

        if (in_array($this--->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
}

Configure Sonata Block

After enabling Sonata Admin Bundle you will need to configure SonataAdminBundle dependencies. SonataAdminBundle also downloads the SonataBlockBundle which is used to create Admin dashboard. So, Now we will configure our SonataBlockBundle into our project. To config this bundle we will use app/config/config.yml file.

# app/config/config.yml

sonata_block:
    default_contexts: [cms]
    blocks:

        # enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts:   [admin]

Import Admin Routing

Sonata Admin Bundle is now properly downloaded and configured. But now if you want to use access your Admin first you need import routing into the main routing.yml.

# app/config/routing.yml
admin_area:
    resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
    prefix: /admin

Install Admin Assets and Cleaning

Now your Admin is ready to use but before go on browser we will run some basic command that basically used after you installed any new bundle.
First is installing bundle assets.

$ php app/console assets:install

and the Second is clear: cache which deletes all older cache from a project

$ php app/console cache:clear

So, Finally, your Admin is ready to use. Now you can view your admin dashboard into a browser.

Write a Reply or Comment

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