简体   繁体   English

Symfony-实施管理面板

[英]Symfony - Implementing Admin Panel

I am just started out with Symfony and stuck with "bundles". 我刚开始使用Symfony并被“捆绑”卡住。 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 : 根据最近发布的Symfony最佳做法

Create only one bundle called AppBundle for your application logic 仅为您的应用程序逻辑创建一个名为AppBundle的捆绑包

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. 当然,可以将例如Admin和Front捆绑包分开,但这仅是为了使代码清晰。

If you're looking for a speedy implementation of an admin panel, SonataAdminBundle is worth a look. 如果您想快速实现管理面板, SonataAdminBundle值得一看。

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. 您可以通过composer安装并导入一些路由,然后从那里定义管理服务以及引用您的ProductsNewsArticle实体的类。

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 ) 作为一个简单的示例(假设使用ORM),安装SonataAdmin后,添加一个服务定义:(例如,取自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. ...以及要匹配的ProductAdmin类。

<?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. 但是,如果这是您的练习,而不是重新发明轮子,SonataAdmin仍然可以作为不错的参考包。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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