Christopher Shennan's Blog

A day in the life of…

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

About Christopher Shennan

I am a web developer specialising in web driven applications using PHP, MySQL, Symfony and Zend and I am currently working for Line Digital in Edinburgh, Scotland.

Most days I can be found frantically coding away with EuroDance in my ears and consuming what I hope to be a never ending supply of coffee... happy days!

Connect with me via Twitter, Google+ or LinkedIn

  • #1 | Written by Jaime about 2 years ago.

    Why don’t you make a PR on github for adding the missing steps of the installation.

    Thanks for sharing!

  • #2 | Written by lopsided about 2 years ago.

    Good work so far! Sonata is awesome but will really benefit from a guide such as this. Any ETA for the next article(s)?

  • #3 | Written by kamal about 2 years ago.

    hi, i need the installation for windows

  • #4 | Written by MediaVince about 2 years ago.

    Hello!
    Nice tut, however the following in the autoload.php file should read: (cf step 3)

    ‘Exporter’ => __DIR__.’/../vendor/bundles/Sonata/exporter/lib’,

    instead of
    ‘Exporter’ => __DIR__.’/../vendor/exporter/lib’,

    I finally got my head around the issue that throws:
    > Fatal error: Class ‘Exporter\Source\DoctrineORMQuerySourceIterator’ not found in…
    when exporting in csv from a sonata admin list.

    Hope that helps!!
    MediaVince

No trackbacks yet.

Leave a Comment

Subscribe to comments

CommentLuv badge