Symfony 2.2 project admin using Sonata admin bundles

Build Symfony admin panel using sonata admin bundles

1 - Download & setup Download Symfony 2.2 package with vendor and configure project with proper write permission for cache, logs directory & vhost to access package in browser.

2 - Check version of your Symfony package

> php app/console -V
> Symfony version 2.2.3 - app/dev/debug

3 - Install required bundles Install sonata-project/block-bundle

> php composer.phar require sonata-project/block-bundle:2.2.3

Install sonata-project/jquery-bundle

> php composer.phar require sonata-project/jquery-bundle:1.8.0

Install knplabs/knp-menu

> php composer.phar require knplabs/knp-menu:1.1.2

Install knplabs/knp-menu-bundle

> php composer.phar require knplabs/knp-menu-bundle:1.1.2

Install sonata-project/exporter

> php composer.phar require sonata-project/exporter:1.3.0

Install sonata-project/admin-bundle

> php composer.phar require sonata-project/admin-bundle:2.2.2

Install sonata-project/doctrine-orm-admin-bundle

> php composer.phar require sonata-project/doctrine-orm-admin-bundle:2.2.*@dev

Install sonata-project/intl-bundle

> php composer.phar require sonata-project/intl-bundle:2.1.*

4 - Alter your config.yml and add following configurations.

# app/config/config.yml
# Sonata block Configuration
sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]
        #sonata.admin_doctrine_orm.block.audit:
        #    contexts:   [admin]
        sonata.block.service.text:
        sonata.block.service.rss:


# knp menu configuration (optional)
knp_menu:
    twig:
        template: knp_menu.html.twig
    templating: false
    default_renderer: twig


# Translator configuration
framework:
    translator: ~


# Sonata intl configuration
sonata_intl:
    timezone:
        # default timezone used as fallback
        default: Europe/Paris


        # locale specific overrides

        locales:
            fr: Europe/Paris
            en_UK: Europe/London

5 - Be sure to enable these bundles in your AppKernel.php file.

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),
        new Sonata\jQueryBundle\SonatajQueryBundle(),
        new Sonata\AdminBundle\SonataAdminBundle(),
        new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
        new Sonata\IntlBundle\SonataIntlBundle(),
        // ...
    );
}

6 - Install the assets from the bundles

> php app/console assets:install web

7 - Now lets add routes for sonata admin bundle.

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

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

8 - Delete cache files

> php app/console cache:clear

9 - Install FOS & sonata-project/user-bundle 9-A :Install friendsofsymfony/user-bundle

> php composer.phar require friendsofsymfony/user-bundle:1.3.2

9-B :Install sonata-project/user-bundle

> php composer.phar require sonata-project/user-bundle:2.1.1

10 - Be sure to enable these bundles in your AppKernel.php file.

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),
        new Sonata\jQueryBundle\SonatajQueryBundle(),
        new Sonata\AdminBundle\SonataAdminBundle(),
        new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
        new Sonata\IntlBundle\SonataIntlBundle(),

    new FOS\UserBundle\FOSUserBundle(),
    new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
    new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),
        // ...
    );
}

11 - Alter config.yml and add configurations for FOS user bundle.

# app/config/config.yml

# FOS user bundle configuration
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Application\Sonata\UserBundle\Entity\User

12 - Generate & configure User bundle 12-A : Generate user bundle This bundle will gives an interface to manage users in application. Execute below command to generate User bundle for you by extending SontaUserBundle.

> php app/console sonata:easy-extends:generate SonataUserBundle

12-B : Register generated user bundle in autoload.php Add below line in autoload.php

$loader->add('Application', __DIR__);

Your autoload.php file looks like below

<?php
    use Doctrine\Common\Annotations\AnnotationRegistry;
    use Composer\Autoload\ClassLoader;

    /**
     * @var $loader ClassLoader
     */
    $loader = require __DIR__.'/../vendor/autoload.php';

    // intl
    if (!function_exists('intl_get_error_code')) {
        require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
    }

    $loader->add('Application', __DIR__);

    AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

    return $loader;

12-C : Register User bundle in app/AppKernel.php Add below line to register generated user bundle.

   new Application\Sonata\UserBundle\ApplicationSonataUserBundle()

    // app/AppKernel.php
    public function registerBundles()
    {
        return array(
        // ...
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),
        new Sonata\jQueryBundle\SonatajQueryBundle(),
        new Sonata\AdminBundle\SonataAdminBundle(),
        new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
        new Sonata\IntlBundle\SonataIntlBundle(),

        new FOS\UserBundle\FOSUserBundle(),
        new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
        new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),
        new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
        // ...
        );
    }

12- D : Add fos and sonata user routes in app/routing.yml

   #app/routing.yml
    fos_user_security:
       resource: "@FOSUserBundle/Resources/config/routing/security.xml"
    fos_user_profile:
       resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
       prefix: /profile
    fos_user_register:
       resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
       prefix: /register
    fos_user_resetting:
       resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
       prefix: /resetting
    fos_user_change_password:
       resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
       prefix: /change-password
    fos_user_security:
       resource: "@FOSUserBundle/Resources/config/routing/security.xml"
    fos_user_profile:
       resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
       prefix: /profile
    fos_user_register:
       resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
       prefix: /register
    fos_user_resetting:
       resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
       prefix: /resetting
    fos_user_change_password:
       resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
       prefix: /change-password

    soanata_user:
       resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml'
       prefix: /admin
    sonata_user_impersonating:
       pattern: /
       defaults: { _controller: SonataPageBundle:Page:catchAll }

12-E : Clear cache

> php app/console cache:clear

13 - Update db schema

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

NOTE : if symfony give 'Unknown column type "json" requested.......' error, add custom type in Doctrine configuration, see below code

# app/config/config.yml
doctrine:
    dbal:
    #for SonataNotificationBundle
    types:
        json: Sonata\Doctrine\Types\JsonType
    #----

14 - Add sonata admin configuration

#app/config/config.yml
sonata_admin:
    title:      <your application title>
    title_logo: /path/to/logo.png

    dashboard:
    blocks:
        #          # display a dashboard block
        - { position: left, type: sonata.admin.block.admin_list }

15 - Lets create super admin user All configurations completed, So lets create super admin user for our newly created Symfony 2.2 admin panel.

> php app/console fos:user:create <username> <email> <password> --super-admin

Now you can access your fully functional Symfony2.2 admin panel using below url URL : http://yourdomain/app_dev.php/admin/dashboard

Thanks