Documentation » Admin

« 22. SonataAdmin without FOSUserBundle/SonataUserBundle

This Page

« 22. SonataAdmin without FOSUserBundle/SonataUserBundle

23. Auto Configuring Admin Classes

If you have a lot of admin classes and don’t want to write all the service definitions or if maybe you don’t like writing configuration and want everything set up for you, this bundle will allow you not to do that.

23.1. Download the SonataAutoConfigureBundle

composer require kunicmarko/sonata-auto-configure-bundle

23.2. How to use

The only thing you need to do is add the configuration for this bundle:

# config/packages/sonata_auto_configure.yaml

        suffix: Admin
        manager_type: orm
            - { namespace: App\Entity, manager_type: orm }
        suffix: Controller
            - App\Controller\Admin


Be sure that the admin directory is included in auto discovery and that autoconfigure is enabled.

This configuration means, find all admin classes, remove the Admin suffix and try to find an entity with the same name in App\Entity namespace and add the orm manager_type. After, try to find the controllers in App\Controller\Admin but replace the Admin suffix for Controller.

If you are interested in more details on how this bundle works, read it here.

23.3. Annotations

Annotations have a higher priority than bundle guesses, so if you are not happy with something, you can add the annotation to your admin class:

namespace App\Admin;

use App\Controller\Admin\CategoryController;
use App\Entity\Category;
use KunicMarko\SonataAutoConfigureBundle\Annotation as Sonata;
use Sonata\AdminBundle\Admin\AbstractAdmin;

 * @Sonata\AdminOptions(
 *     label="Category",
 *     managerType="orm",
 *     group="Category",
 *     showInDashboard=true,
 *     keepOpen=true,
 *     onTop=true,
 *     icon="<i class='fa fa-user'></i>",
 *     labelTranslatorStrategy="sonata.admin.label.strategy.native",
 *     labelCatalogue="App",
 *     pagerType="simple",
 *     controller=CategoryController::class,
 *     entity=Category::class,
 *     adminCode="admin_code",
 *     autowireEntity=true,
 * )
final class CategoryAdmin extends AbstractAdmin

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