Documentation » Admin

3. Configuration »

« 1. Installation

2. Getting started with SonataAdminBundle

After installation of SonataAdminBundle you need to configure it for your models. Here is a quick checklist of what is needed to quickly setup SonataAdminBundle and create your first admin interface for the models of your application:

  • Step 1: Define SonataAdminBundle routes
  • Step 2: Setup the persistency service (ORM, ODM, ...)
  • Step 3: Create admin class
  • Step 4: Create admin service
  • Step 5: Configuration
  • Step 6: Security

2.1. Step 1: Define SonataAdminBundle routes

SonataAdminBundle contains several routes. Import them by adding the following code to your application’s routing file:

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

    resource: .
    type: sonata_admin
    prefix: /admin


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).

At this point you can already access the admin dashboard by visiting the url: http://yoursite.local/admin/dashboard.

2.2. Step 2: Setup the persistence service (ORM, ODM, ...)

SonataAdminBundle does not impose any persistency services (service for handling and controlling your models), however most likely your application will use some persistency services (like ORM or ODM for database and document stores) therefore you can use the following bundles officially supported by Sonata Project’s admin bundle:

  • SonataDoctrineORMAdminBundle
  • SonataDoctrineMongoDBAdminBundle
  • SonataDoctrinePhpcrAdminBundle

Propel users are warmly welcome to contribute and create a new bundle for Propel ORM that will be integrated in SonataAdminBundle.

Install a persistency service you need and configure it according to their related documentation.

2.3. Step 3: Create Admin class

Generate YourNSNewsBundle with “php app/console generate:bundle”. Admin class represents mapping of your model and administration sections (forms, list, show). The easiest way to create an admin class for your model is to extend the Sonata\AdminBundle\Admin\Admin class. For filter, list and show views, you can target a sub model property thanks to the dot-separated notation (eg: mySubModel.mySubSubModel.myProperty).

Here is a simple example from the SonataNewsBundle:

namespace YourNS\NewsBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class TagAdmin extends Admin
    protected function configureFormFields(FormMapper $formMapper)
            ->add('enabled', null, array('required' => false))

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)

    protected function configureListFields(ListMapper $listMapper)

2.4. Step 4: Create admin service

To notify your administration of your new admin class you need to create an admin service and link it into the framework by setting the sonata.admin tag.

Create either a new admin.xml or admin.yml file inside the MyBundle/Resources/config/ folder:

<!-- MyBundle/Resources/config/admin.xml -->
<container xmlns=""
       <service id="sonata.admin.tag" class="YourNS\NewsBundle\Admin\TagAdmin">
          <tag name="sonata.admin" manager_type="orm" group="Posts" label="Blog"/>
          <argument />
          <call method="setTranslationDomain">
# MyBundle/Resources/config/admin.yml
        class: YourNS\NewsBundle\Admin\TagAdmin
            - { name: sonata.admin, manager_type: orm, group: posts, label: "Blog" }
            - ~
            - YourNS\AdminBundle\Entity\Course
            - 'SonataAdminBundle:CRUD'
            - [ setTranslationDomain, [YourNSAdminBundle]]

Now include your new configuration file in the framework (make sure that your resource value has the correct file extension depending on the code block that you used above):

# app/config/config.yml
    - { resource: @MyBundle/Resources/config/admin.xml }

Or you can load the file inside with the Bundle’s extension file using the load() method as described in the symfony cookbook.

# YourNS/AdminBundle/DependencyInjection/YourNSAdminBundleExtension.php for XML configurations

use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\FileLocator;

public function load(array $configs, ContainerBuilder $container) {
    // ...
    $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
# YourNS/AdminBundle/DependencyInjection/YourNSAdminBundleExtension.php for YAML configurations

use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\FileLocator;

public function load(array $configs, ContainerBuilder $container) {
    // ...
    $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

2.5. Step 5: Configuration

At this point you have basic administration for your model. If you wish to quickly customize your administration you can create some configuration options and change them according to your requirements:

# app/config/config.yml
    title:      Sonata Project
    title_logo: /bundles/sonataadmin/logo_title.png
        # default global templates
        layout:  SonataAdminBundle::standard_layout.html.twig
        ajax:    SonataAdminBundle::ajax_layout.html.twig

        # default actions templates, should extend a global templates
        list:    SonataAdminBundle:CRUD:list.html.twig
        show:    SonataAdminBundle:CRUD:show.html.twig
        edit:    SonataAdminBundle:CRUD:edit.html.twig

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

Linking the admin class to the dashboard is done automatically because of the default option you defined above:

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

However you can define only admin groups you want to show in the dashboard by:

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

            label: Page
            items: ~

More information can be found in the configuration chapter of this documentation.

2.6. Step 6: Security

The last important step is security. By default, the SonataAdminBundle does not come with any user management for ultimate flexibility, however it is most likely your application requires such feature. The Sonata Project includes a SonataUserBundle which integrates the very popular FOSUserBundle. Please refer to the security section of this documentation for more information.

That should be it! Read next sections fore more verbose documentation of the SonataAdminBundle and how to tweak it for your requirements.

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