Home » Php » php – sonata admin bundle symfony

php – sonata admin bundle symfony

Posted by: admin July 12, 2020 Leave a comment

Questions:

I tried to install sonata admin bundle to administrate my users.

I use FOS user bundle.

I have folowed the instructions, but anything went wrong and I don’t find what.

I have the error:

Cannot automatically determine base route name, please define a default baseRouteName value for the admin class UserBundle\Admin\UserAdmin in C:\Users\Alexandre\hubiC\www\questionnaire\app/config. (which is being imported from “C:\Users\Alexandre\hubiC\www\questionnaire\app/config\routing.yml”).

In my service I have:

services:
    sonata.admin.user:
        class: UserBundle\Admin\UserAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Content", label: "User" }
        arguments:
            - ~
            - UserBundle\Entity\User
            - ~
        calls:
            - [ setTranslationDomain, [UserBundle]]

In my config:

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
    - { resource: @UserBundle/Resources/config/admin.yml }
sonata_block:
    default_contexts: [cms]
    blocks:
        # Enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts:   [admin]
        # Your other blocks

And the file UserAdmin:

<?php // 
namespace UserBundle\Admin;

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

class UserAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('nom')
            ->add('prenom')
            ->add('adresse')
            ->add('npa')
            ->add('localite')
            ->add('entreprise')
        ;
    }

    // Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('nom')
            ->add('prenom')
            ->add('adresse')
            ->add('npa')
            ->add('localite')
            ->add('entreprise')
        ;
    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('nom')
            ->add('prenom')
            ->add('adresse')
            ->add('npa')
            ->add('localite')
            ->add('entreprise')
        ;
    }
}

This file in the folder UserBundle/Admin.

What was wrong?

Thanks

How to&Answers:

I’m not sure, why sonata doesn’t automatically generate baseRouteName for you. I suppose that you define your custom directory structure or custom class name. You can dump return of getBaseRouteName method. This method is used to generate routing information.

You can also define it (not automatically).:

   protected $baseRouteName = 'your_name'; 
   protected $baseRoutePattern = 'your_name';

You can check routers in console by app/console router:debug, your new route from admin should be there

Routing problematic is described here in documentation:
https://sonata-project.org/bundles/admin/2-3/doc/reference/routing.html