2
votes

I followed the tutorial of sonata admin and it does generate the menu but everything is under a "default" group. I would like to add new groups like "users", "events" and so on.

I try to do that found on https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_knp_menu.html#extending-the-menu :

<?php
namespace AppBundle\EventListener;

use Sonata\AdminBundle\Event\ConfigureMenuEvent;

/**
 * Description of AdminMenuBuilderListener
 *
 * @author christophe
 */
class AdminMenuBuilderListener {
    public function addMenuItems(ConfigureMenuEvent $event)
    {
        $menu = $event->getMenu();

        $child = $menu->addChild('reports', array(
            'route' => 'admin_app_category_index',
            'labelAttributes' => array('icon' => 'fa fa-bar-chart'),
        ));

        $child->setLabel('Daily and monthly reports');
    }
}

routing.yml

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
    prefix: /user

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

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

services.yml

parameters:
#    parameter_name: value

services:
    admin.category:
        class: AppBundle\Admin\CategoryAdmin
        arguments: [~, AppBundle\Entity\Category, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Category }

    admin.blog_post:
        class: AppBundle\Admin\BlogPostAdmin
        arguments: [~, AppBundle\Entity\BlogPost, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Blog post }

    app.menu_listener:
        class: AppBundle\EventListener\AdminMenuBuilderListener
        tags:
            - { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: addMenuItems }

CategoryAdmin.php

<?php

namespace AppBundle\Admin;

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

class CategoryAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('name', 'text');
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper->add('name');
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper->addIdentifier('name');
    }
}

It actually throws me the exception : Unable to generate a URL for the named route "admin_app_category_index" as such route does not exist. I am very new to symfony and I do not know how the routes are generated in this case.

Any help is welcome.

1

1 Answers

1
votes

Actually, there is a really more simple way to achieve that :

services:
    admin.category:
        class: AppBundle\Admin\CategoryAdmin
        arguments: [~, AppBundle\Entity\Category, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Category , group : Blog}

    admin.blog_post:
        class: AppBundle\Admin\BlogPostAdmin
        arguments: [~, AppBundle\Entity\BlogPost, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Blog post, group : Blog }

    admin.user:
        class: AppBundle\Admin\UserAdmin
        arguments: [~, AppBundle\Entity\User, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: List, group : Users }

    admin.group:
        class: AppBundle\Admin\GroupAdmin
        arguments: [~, AppBundle\Entity\group, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Groups, group : Users  }

You can see the group that permits to add group in the admin menu.