Symfony CMS - day 03 - Sonata Admin Installation

Oct 3rd, 2013

Installing Sonata Admin Bundle.

Third day of our Build CMS Application with Symfony articles suite.

Previous article Behavior Driven Development with Behat and Mink.

Symfony standard Edition is not enough to build a small Content Management System. In this articles suite, we will discover how to use some of the most powerful Sonata Bundles. Today : Sonata Admin Bundle.

Write the tests

As we have Behat and Mink installed, we can write tests for Sonata Admin install.

touch src/My/BDDBundle/Features/02-sonata-admin-install.feature
@sonataAdmin
Feature: sonata admin installation
  In order to manage my application's datas
  As a smart developer
  I need to be able to see the sonata admin home page

  Scenario: Go to Sonata admin home  page
    Given I am on "/admin/dashboard"
    Then I should see "My Admin Dashboard"

The @sonataAdmin line is a tag. We can call bin/behat and just play feature or scenario associated with this tag. See Gherkin tags documentation and Gherkin Filters documentation.

# play all the features
bin/behat

# play only @sonataAdmin tagged features or scenarios
bin/behat --tags="@sonataAdmin"

Nevertheless, we get an error, as Sonata admin is not installed yet.

Install Sonata Admin Bundle

In your composer.json file, add :

[...]
    "require": {
        [...],
        "sonata-project/admin-bundle": "dev-master",
        "sonata-project/intl-bundle": "2.2.*@dev",
        "sonata-project/doctrine-orm-admin-bundle": "2.2.*@dev",
        "simplethings/entity-audit-bundle": "0.*@dev",
        "sonata-project/block-bundle": "2.2.*@dev",
        "sonata-project/jquery-bundle": "1.8.*",
        "sonata-project/cache-bundle": "2.1.*@dev",
        "knplabs/knp-menu-bundle": "1.1.*@dev"
    },
[...]

Then run a

composer update

Sonata Admin Bundle configuration

Sonata Block Bundle

# app/config/config.yml
[...]
sonata_admin:
    title:      My Admin Dashboard
    dashboard:
        blocks:
            - { position: left, type: sonata.admin.block.admin_list }

sonata_block:
    default_contexts: [cms]
    blocks:
        # Enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts:   [admin]

        sonata.block.service.text:
        sonata.page.block.container:
        sonata.page.block.children_pages:

routing

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

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

Kernel

Enable our new bundles in the kernel :

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...

        // Sonata admin dependencies
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Sonata\jQueryBundle\SonatajQueryBundle(),
        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(),
        new SimpleThings\EntityAudit\SimpleThingsEntityAuditBundle(),

        // Then add SonataAdminBundle
        new Sonata\AdminBundle\SonataAdminBundle(),
        // ...
    );
}

Note : We add SimpleThingsEntityAuditBundle because it’s a non declared dependancy of SonataDoctrineORMAdminBundle (see SonataDoctrineORMAdminBundle git). If you don’t add it, it produce a Catchable Fatal Error: Argument 3 passed to Sonata\DoctrineORMAdminBundle\Block\AuditBlockService::__construct() must be an instance of SimpleThings\EntityAudit\AuditReader, null given, blah blah when later creating block in Sonata page.

Publishing web assets + some cleanup

app/console assets:install web --symlink
app/console cache:clear

Passing tests

bin/behat --tags="@sonataAdmin"

Greeeen !

Sonata Admin is now installed. Not usable to manage you application yet, but installed and working.

See you soon.

Next article will be on Sonata User Installation


Comments

comments powered by Disqus