Version

Documentation » Admin

2. Creating an Admin »

« Admin Bundle

1. Installation

SonataAdminBundle is just a bundle and as such, you can install it at any moment during a project’s lifecycle.

1.1. 1. Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require sonata-project/admin-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

1.2. 1.1. Download a Storage Bundle

You’ve now downloaded the SonataAdminBundle. While this bundle contains all functionality, it needs storage bundles to be able to communicate with a database. Before using the SonataAdminBundle, you have to download one of these storage bundles. The official storage bundles are:

You can download them in the same way as the SonataAdminBundle. For instance, to download the SonataDoctrineORMAdminBundle, execute the following command:

$ composer require sonata-project/doctrine-orm-admin-bundle

Tip

Don’t know which to choose? Most new users prefer SonataDoctrineORMAdmin, to interact with traditional relational databases (MySQL, PostgreSQL, etc).

1.3. Step 2: Enable the Bundle

Then, enable the bundle and the bundles it relies on by adding the following line in the app/AppKernel.php file of your project:

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            // The admin requires some twig functions defined in the security
            // bundle, like is_granted. Register this bundle if it wasn't the case
            // already.
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),

            // These are the other bundles the SonataAdminBundle relies on
            new Sonata\CoreBundle\SonataCoreBundle(),
            new Sonata\BlockBundle\SonataBlockBundle(),
            new Knp\Bundle\MenuBundle\KnpMenuBundle(),

            // And finally, the storage and SonataAdminBundle
            new Sonata\AdminBundle\SonataAdminBundle(),
        );

        // ...
    }

    // ...
}

Note

If a bundle is already registered somewhere in your AppKernel.php, you should not register it again.

Note

Since version 2.3, the bundle comes with jQuery and other front-end libraries. To update the versions (which isn’t required), you can use Bower. To make sure you get the dependencies that match the version of SonataAdminBundle you are using, you can make bower use the local bower dependency file, like this:

$ bower install ./vendor/sonata-project/admin-bundle/bower.json

1.4. Step 3: Configure the Installed Bundles

Now all needed bundles are downloaded and registered, you have to add some configuration. The admin interface is using SonataBlockBundle to put everything in blocks. You just have to tell the block bundle about the existence of the admin block:

# app/config/config.yml
sonata_block:
    default_contexts: [cms]
    blocks:
        # enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts: [admin]
        # ...

Note

Don’t worry too much if, at this point, you don’t yet understand fully what a block is. The SonataBlockBundle is a useful tool, but it’s not vital that you understand it in order to use the admin bundle.

1.5. Step 4: Import Routing Configuration

The bundles are now registered and configured correctly. Before you can use it, the Symfony router needs to know the routes provided by the SonataAdminBundle. You can do this by importing them in the routing configuration:

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

1.6. Step 5: Enable the “translator” service

The translator service is required by SonataAdmin to display all labels properly.

# app/config/config.yml
framework:
    translator: { fallbacks: [en] }

For more information: http://symfony.com/doc/current/translation.html#configuration

1.7. Step 6: Define routes

To be able to access SonataAdminBundle’s pages, you need to add its routes to your application’s routing file:

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

Note

If you’re using XML or PHP to specify your application’s configuration, the above routing configuration must be placed in routing.xml or routing.php according to your format (i.e. XML or PHP).

Note

For those curious about the resource: . setting: it is unusual syntax but used because Symfony requires a resource to be defined (which points to a real file). Once this validation passes Sonata’s AdminPoolLoader is in charge of processing this route and it simply ignores the resource setting.

At this point you can already access the (empty) admin dashboard by visiting the URL: http://yoursite.local/admin/dashboard.

1.8. Step 7: Preparing your Environment

As with all bundles you install, it’s a good practice to clear the cache and install the assets:

$ php bin/console cache:clear
$ php bin/console assets:install

1.9. The Admin Interface

You’ve finished the installation process, congratulations. If you fire up the server, you can now visit the admin page on http://localhost:8000/admin

Note

This tutorial assumes you are using the build-in server using the php bin/console server:start (or server:run) command.

../_images/getting_started_empty_dashboard.png

As you can see, the admin panel is very empty. This is because no bundle has provided admin functionality for the admin bundle yet. Fortunately, you’ll learn how to do this in the next chapter.

Found a typo? Something is wrong in this documentation? Just fork and edit it!