avris/micrus-crud

Micrus框架的CRUD生成器

v4.0.0 2018-01-28 10:17 UTC

This package is auto-updated.

Last update: 2024-08-29 03:20:57 UTC


README

这是一个为Micrus框架提供的模块,可以根据简单的配置生成管理面板。

CRUD代表“创建、读取、更新和删除”。此模块为您提供了一个工具,可以自动生成一个管理面板,用于在数据库对象(Doctrine实体)上执行这些操作,以及列出它们(具有分页、过滤和排序)。

安装

运行

composer require avris/micrus-crud

然后在您的App\App:registerModules中注册该模块

yield new \Avris\Micrus\Crud\CrudModule;

在前端方面,Crud的视图使用BootstrapFont Awesome 5

配置

要为特定模型生成CRUD,只需创建一个扩展Avris\Micrus\Crud\Controller\CrudController的控制器

/**
 * @Crud(
 *     "App\Entity\User",               -- class of the model to be handled by this controller
 *     form="App\Form\AdminUserForm",   -- form class (not required if edit & new are disabled)
 *     icon="fas fa-users",
 *     perPage=5,                       -- default 100
 *     metrics={                        -- what metrics to display in the dashboard
 *        "all": @CrudMetric("Avris\Micrus\Crud\Metric\CountMetric"),
 *        "lastWeek": @CrudMetric("Avris\Micrus\Crud\Metric\CountMetric", filters={"createdAt":">@ -1 week"}),
 *        "postsAvg": @CrudMetric("Avris\Micrus\Crud\Metric\AvgCountMetric", relation="posts"),
 *     },
 *                                      -- by default routes "list", "new", "edit", "show", "delete" are generated for all models
 *     disableRoutes={"new"},           -- unless some is removed like this
 *     addRoutes={"email": "/{__restr__:id}/email"} -- or added like this
 * )
 */
class UserController extends CrudController
{
    protected function configureList(ListConfig $config)
    {
        $config
            ->add('username', true)
            ->add('email', false, false)
            ->add('postsCount', false, false, false)
            ->add('roleName')
            ->add('createdAt')
            ->addAction('Crud/testEmail')
            ->addAction('Crud/impersonate')
        ;
    }

    protected function configureExport(ExportConfig $config)
    {
        $config
            ->add('username')
            ->add('email')
            ->add('postsCount')
            ->add('roleName')
        ;
    }

    protected function configureShow(ShowConfig $config)
    {
        $config
            ->add('username')
            ->add('email')
            ->add('roleName')
            ->add('posts', 'Post')
            ->add('createdAt')
        ;
    }

    public function emailAction(User $user, MailBuilder $mailBuilder, Mailer $mailer)
    {
        // implement like any other action
    }
}

config/crud.yml中设置通用配置(非模型特定)

baseRoute: admin        # what all the generated routes should start with ("/admin/post/list")
perPage: 100            # number of items per page in the list view
dashboard: true         # enable/disable dashboard
idRestriction: uuid     # determines the restriction od ID in the generated routes (= whether to use "/admin/post/{uuid:id}/edit" or "/admin/post/{int:id}/edit" or something else)
cruds: {}               # instead of in the annotations, the model-specific config can be put here

控制器可以提供方法来指定

  • 在列表视图中使用哪些字段(configureList
  • 在导出到文件中(configureExport),
  • 在显示视图中(configureShow),
  • 创建实体时使用哪个表单(getNewForm),
  • 以及编辑它时(getEditForm),
  • 是否需要在顶部添加任何额外的按钮(configureGeneral)。
  • 另外,如果您添加了一些自定义路由,显然您需要处理它们(这里的是emailAction)。

列表视图

配置列表视图时,您可以使用具有以下参数的add添加列

  • name - 属性名称 (必需)
  • key - (默认: false)
    • false = 仅显示值
    • true = 显示值为其详细信息的链接
    • string = 显示值为{$key}_show的链接
  • sortable - 布尔值 (默认: false)
  • filterable - (默认: true)
    • true = 启用标准过滤
    • false = 禁用过滤
    • array = 使用指定数组值的下拉菜单进行过滤
    • string = 使用下拉菜单进行过滤,下拉菜单包含指定在$filterable中指定的类型的所有实体
  • view - 您可以通过更改此字段和创建相应的模板来覆盖表格单元格的渲染方式 (默认: Crud/Show/element)
  • label - (默认 l("entity:{$modelName}.fields.{$fieldName}"))

您还可以使用addAction($template)向最后一列添加新操作,提供要在此处渲染的模板的名称。

显示视图

配置显示视图时,您可以使用具有以下参数的add添加行

  • name - 属性名称 (必需)
  • key - (默认: false)
    • false = 仅显示值
    • true = 显示值为其详细信息的链接
    • string = 显示值为{$key}_show的链接
  • row - 不转义值,布尔值 (默认: false)
  • view - 您可以通过更改此字段和创建相应的模板来覆盖表格单元格的渲染方式 (默认: Crud/Show/element)
  • label - (默认 l("entity:{$modelName}.fields.{$fieldName}"))

常规设置

您可以使用addAction($template)向顶部栏添加新操作,提供要在此处渲染的模板的名称。

导出设置

可以将元素列表导出到文件(内置格式:csvjsonxml)。

configureExport 方法中,您可以定义一个给定模型的可导出字段列表。函数 add 的第二个参数可以是一个可调用的 ($entity, string $field) -> string,您可以使用它来个性化输出(默认情况下使用简单的getter)。

钩子

CrudController 的以下方法将在实体生命周期中的特定时刻执行。只需覆盖它们来挂钩到这些时刻。

create()
getNewForm($entity)
getEditForm($entity)
getForm($entity)

preCreate($entity)
postCreate($entity)
preUpdate($entity)
postUpdate($entity)
preDelete($entity)
postDelete($entity)

自定义视图

您可以在您的 templates 目录中简单地覆盖它来调整Crud的模板。

要列出所有可用的CRUD,可以使用以下代码

{% for crud, options in cruds if routeExists(crud ~ '_list') %}
    <li>
        <a href="{{ route(crud ~ '_list') }}">
            <span class="fa fa-fw {{ options.icon }}"></span>
            {{ ('entity:'~options.model~'.plural')|l }}
        </a>
    </li>
{% endfor %}

链接到仪表板

<a href="{{ route('admin_dashboard') }}">
    <span class="fa fa-dashboard"></span>
</a>

版权