Installing the Sonata Admin Bundle

By

29th March 2012 | No Comments

The second part of this Sonata Bundles mini series takes us onto installing the SonataAdmin bundle.  This pretty much follows the instructions in the official documentation but required a few additional steps in order to get it up and running.  The basic process I followed is detailed below.

Step 1 - Update the deps file to install the necessary vendor bundles

Add the following lines to the deps file of your Symfony2 project

[SonataAdminBundle]
    git=http://github.com/sonata-project/SonataAdminBundle.git
    target=/bundles/Sonata/AdminBundle

[SonataBlockBundle]
    git=http://github.com/sonata-project/SonataBlockBundle.git
    target=/bundles/Sonata/BlockBundle

[SonataCacheBundle]
    git=http://github.com/sonata-project/SonataCacheBundle.git
    target=/bundles/Sonata/CacheBundle

[SonatajQueryBundle]
    git=http://github.com/sonata-project/SonatajQueryBundle.git
    target=/bundles/Sonata/jQueryBundle

[KnpMenuBundle]
    git=http://github.com/KnpLabs/KnpMenuBundle.git
    target=/bundles/Knp/Bundle/MenuBundle

[KnpMenu]
    git=http://github.com/KnpLabs/KnpMenu.git
    target=/knp/menu

[Exporter]
    git=http://github.com/sonata-project/exporter.git
    target=/exporter

Step 2 - Install the vendor bundles

Run the following command from a shell prompt in the root of your Symfony2 project
php bin/vendors install

Step 3 - Update autoload.php

Edit /app/autoload.php and add the following lines
$loader->registerNamespaces(array(
    // ...
    'Sonata'     => __DIR__.'/../vendor/bundles',
    'Exporter'   => __DIR__.'/../vendor/exporter/lib',
    'Knp\Bundle' => __DIR__.'/../vendor/bundles',
    'Knp\Menu'   => __DIR__.'/../vendor/knp/menu/src',
    // ...
));

Step 4 - Update AppKernel.php

Edit /app/AppKernel.php and add the following lines
public function registerBundles()
{
    return array(
        // ...
        new Sonata\AdminBundle\SonataAdminBundle(),
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Sonata\CacheBundle\SonataCacheBundle(),
        new Sonata\jQueryBundle\SonatajQueryBundle(),
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),
        // ...
    );
}

Step 5 - Updating the config.app file

Now that we have installed the SonataBlockBundle we need to add basic configuration for it otherwise Symfony will complain. This can be done by adding the following lines to your app/config/config.yml file
# app/config/config.yml
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.action:
        sonata.block.service.rss:

        # Some specific block from the SonataMediaBundle
        #sonata.media.block.media:
        #sonata.media.block.gallery:
        #sonata.media.block.feature_media:
The official documentation also states we need to add the translator configuration value if we wish to use the default translation i.e. have the admin show proper titles and labels rather than page titles like "title_dashboard".  The documentation suggests that you add in the following to your /app/config.yml file
framework:
    translator: ~
but I found the following more robust
framework:
    translator:      { fallback: en }
Since we are editing the config.yml file now I would suggest putting it in while we remember :)

Step 6 - Add the routing.yml Configuratino

We need to add the appropriate configuration into our routing file so we can use the SonataAdmin bundle.  Add the following into /app/config/routing.yml
admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin

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

Step 7 - Install the bundle assets

Run the following command from a shell prompt in the root of your Symfony2 project
php app/console assets:install web --symlink

Step 8 - Clear the cache

Run the following command from a shell prompt in the root of your Symfony2 project
php app/console cache:clear

Step 9 - Access The Admin

That should be everything we need to do to get the SonataAdmin bundle setup and ready to access.  If this has been done correctly you should be able to go to the admin URL for your Symfony2 project i.e. http://symfony2.chris.home.internal/admin (assuming we are using the same configuration we used in Part 1 - Installing and Configuring Symfony2) and you should be presented with a screen similar to:-

There is not much we can do with it at the moment as we have not created any models yet and we will cover that in the next post.

Sources

SonataAdminBundle Installation Documentation

http://sonata-project.org/bundles/admin/master/doc/reference/installation.html

SonataBlockBundle Installation Documentation

http://sonata-project.org/bundles/block/master/doc/reference/installation.html

Comments

comments powered by Disqus