简体   繁体   中英

Symfony - Implementing Admin Panel

I am just started out with Symfony and stuck with "bundles". Suppose I want to implement an admin panel which allows the administrator to:

  1. Manage Products
  2. Manage Articles
  3. Manage News

How do I go about implementing such an admin panel? Should the admin panel be one bundle with separate controllers for products/news/articles? Or should I put products/news/articles into bundles of their own and then (maybe) group them into an "admin" bundle (not sure if that is possible).

According to recently released Symfony Best Practices :

Create only one bundle called AppBundle for your application logic

Create separated bundles, only they can be reused as a stand-alone piece of software. Of course, It's possible to separate for example Admin and Front bundle, but only for clarity of code.

If you're looking for a speedy implementation of an admin panel, SonataAdminBundle is worth a look.

You install via composer & import some routes, and from there, it's a matter of defining admin services, and classes that reference your Products , News and Article entities.

I like it because it's extensible & very quick to get set up. It can also handle relationships between entities by embedding one admin interface in another right out of the box, in addition to filter forms, and enabling / disabling routes on a per-entity basis. Also, the twig templates are super-modular, and can be overridden easily.

As a quick example (which assumes ORM), once you've installed SonataAdmin, add in a service definition: (ex. taken from Sonata Admin Docs )

services:
    sonata.admin.pprodut:
        class: Acme\DemoBundle\Admin\ProductAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Content", label: "Product" }
        arguments:
            - ~
            - Acme\DemoBundle\Entity\Product
            - ~

... and a ProductAdmin class to match.

<?php
// src/Acme/DemoBundle/Admin/ProductAdmin.php

namespace Acme\DemoBundle\Admin;

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

class ProductAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('title', 'text', array('label' => 'Product Title'))
            ->add('sku', 'text')
            ->add('description') //if no type is specified, SonataAdminBundle tries to guess it
            // Other fields ...
        ;
    }

    // Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('title')
            ->add('sku')
        ;
    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('title')
            ->add('sku')
        ;
    }
}

And you're off to the races.

However, if this is an exercise for you, rather than a possible reinvention of the wheel, SonataAdmin can still serve as a nice reference bundle. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM