Cómo instalar SonataAdmin en Symfony 4

Pasos para instalar el panel de administración Sonata Admin

Image for post
Image for post

Paso 0

Paso 1. Instalar SonataAdminBundle

composer require sonata-project/admin-bundle
Sonata\DatagridBundle\SonataDatagridBundle::class => [‘all’ => true],Sonata\CoreBundle\SonataCoreBundle::class => [‘all’ => true],Sonata\BlockBundle\SonataBlockBundle::class => [‘all’ => true],Knp\Bundle\MenuBundle\KnpMenuBundle::class => [‘all’ => true],Sonata\AdminBundle\SonataAdminBundle::class => [‘all’ => true],
sonata_admin:  title: 'Sonata Admin'  dashboard:    blocks:      - { type: sonata.admin.block.admin_list, position: left }sonata_block:  blocks:    sonata.admin.block.admin_list:      contexts: [admin]
title_logo: bundles/sonataadmin/logo_title.png
sonata_core:  form:    mapping:      enabled: false
admin_area:  resource:    "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"  prefix: /admin_sonata_admin:  resource: .  type: sonata_admin  prefix: /admin

Paso 2. Configurar SonataAdminBundle

translator: { fallbacks: ['%locale%'] }
bin/console cache:clear
bin/console assets:install

Paso 3. Instalar SonataDoctrineORMAdminBundle

composer require sonata-project/doctrine-orm-admin-bundle
Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle::class => ['all' => true]

Paso 4. Creando nuestro primer Admin

Sonata\AdminBundle\Admin\AbstractAdmin
<?phpnamespace App\Admin;use Sonata\AdminBundle\Admin\AbstractAdmin;use Sonata\AdminBundle\Datagrid\ListMapper;use Sonata\AdminBundle\Datagrid\DatagridMapper;use Sonata\AdminBundle\Form\FormMapper;use Symfony\Component\Form\Extension\Core\Type\TextType;use Symfony\Component\Form\Extension\Core\Type\TextareaType;final class ProjectAdmin extends AbstractAdmin {  protected function configureFormFields(FormMapper $formMapper) {    $formMapper      ->add('name', TextType::class, ['label' => 'Nombre'])      ->add('description', TextareaType::class, ['label' =>      'Descripción']);  }  protected function configureDatagridFilters(DatagridMapper  $datagridMapper)  {    $datagridMapper->add('name', null, ['label' => 'Nombre']);  }  protected function configureListFields(ListMapper $listMapper) {    $listMapper->addIdentifier('name', null, ['label' => 'Nombre']);  }}
services:  admin.project:    class: App\Admin\ProjectAdmin    arguments: [~, App\Entity\Project, ~]    tags:      - { name: sonata.admin, manager_type: orm, label: Proyecto, group: GroupName }
imports:  - { resource: "admin.yaml" }

Paso 5. Automatizando la creación de clases Admin

composer require symfony/maker-bundle --dev
make:sonata:admin
composer require kunicmarko/sonata-auto-configure-bundle
# config/packages/sonata_auto_configure.yamlsonata_auto_configure:
admin:
suffix: Admin
manager_type: orm
entity:
namespaces:
- { namespace: App\Entity, manager_type: orm }
controller:
suffix: Controller
namespaces:
- App\Controller\Admin

¿Quieres recibir más artículos como este?

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store