antoniosam/easypanelbundle

PHP 库。

v3.0 2020-04-02 23:31 UTC

README

###已移除 symfony 4 和 3 的所有支持

用于创建管理员的简化支持捆绑包,简化了列表、新增、编辑、显示(详情)等简单任务。

开始使用

此捆绑包仅提供创建用于管理面板基本操作(如列表、新增、编辑、显示)的模板的帮助和支持,不会为框架创建新组件,而是使用 symfony 的组件。

  • 渲染(twig)
  • 表单接口
  • 路由 查询信息显示的查询未包含在捆绑包中,依赖于用户。

安装

composer require antoniosam/easypanelbundle

然后在 AppKernel 中添加捆绑包

 $bundles = [
       Ast\EasyPanelBundle\EasyPanelBundle::class => ['all' => true],
       ...
      ]

CRUD 命令

为了简化面板的创建,包括自动生成具有预定义功能的控制器和表单的方法,可以是所有实体或逐个生成。

可选

  • folder: 在项目结构中的文件夹内创建的文件夹
  • prefix: 控制器路由的前缀(可选,默认为空) 参数不应包含 src 文件夹,默认集成

create:panel

在指定的文件夹内查找所有实体,并创建所有控制器和表单

php bin/console easypanel:create:panel nombre_proyecto directorio_entitys tipo
php bin/console easypanel:create:panel "Admin Mascotas" Entity(equal = /src/Entity) html
php bin/console easypanel:create:panel Demo Entity html --folder=admin --prefix=admin
php bin/console easypanel:create:panel Demo Entity api --folder=api --prefix=api
php bin/console easypanel:create:panel "Admin Mascotas" Entity --clase_login=usuario
ej.
php bin/console easypanel:create:panel "Admin Mascotas" Entity html --folder=admin --prefix=admin --clase_login=usuario

create:menu

创建一个包含找到的实体所有路由的 Twig 文件,以便可以将其导入配置中

php bin/console easypanel:create:menu Demo Entity

create:modulo

通过 namespace 选择一个实体,创建其控制器和表单

php bin/console easypanel:create:modulo App\Entity\Mascota
php bin/console easypanel:create:modulo App\Entity\Mascota api --folder=api --prefix=api
Output
/project_root/src/Controller/MascotaController.php
/project_root/src/Controller/Api/MascotaController.php

install:assets

解压缩包含面板 css 和 js 资源的 Zip 文件(在配置中更改面板类型)

easypanel:install:assets --tipo=material o  sb-admin

登录

注意 建议在设置安全之前首先创建管理员

此捆绑包仅包括屏幕和控制器,安全配置应在 security.yml 文件中完成

捆绑包中包含服务 app.custom_encoder

security.yml

security:
    encoders:
        App\Entity\Administrador:
            id: app.custom_encoder
            ...
    providers:
        entity_admin:
            entity:
                class:  App\Entity\Administrador
                property: correo
        ...
    firewalls:
        admin_area:
            pattern: /admin.*
            provider: entity_admin
            anonymous: ~
            form_login:
                login_path: /admin/login/
                check_path: /admin/login/
                default_target_path:  /admin/
            logout:
                path:   /admin/login/salir
                target: /admin/

        main:
           ....
    access_control:
        - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/*,       roles: IS_AUTHENTICATED_FULLY }
        ....

在命令 create:panel 中生成类 EasyPanelLoginFormAuthenticator

在 src/Security/[dir]/EasyPanelLoginFormAuthenticator 文件夹中,并应将其添加到防火墙中

dir 是命令中使用的文件夹名称

例如:

    firewalls:
        admin_area:
            pattern: /admin.*
            provider: entity_admin
            anonymous: ~
            form_login:
                login_path: /admin/login/
                check_path: /admin/login/
                default_target_path:  /admin/
            logout:
                path:   /admin/login/salir
                target: /admin/
            guard:
                authenticators:
                    - App\Security\[dir]\EasyPanelLoginFormAuthenticator

服务

允许配置视图,包括自定义菜单,更改整体布局(视图所扩展的布局),包括项目名称,以及更多配置。

easy_panel:
    layoutpanel: Layout que extiende la vista (Default: @EasyPanel/layoutmaterial.html.twig)
    viewpanel: es el template que se usa para generar las vistas (Default: @EasyPanel/viewmaterial.html.twig)
    layoutlogin: Es el layout que se usa para mostrar el formulario de sesion (Default: @EasyPanel/loginlayout.html.twig))
    viewmenu: Es el template que se incluira en la seccion de menu  (Default: @EasyPanel/Default/menumaterial.html.twig)
    nombreproyecto: Nombre del proyecto (Default: '')
    rutalogout: ruta de symfony para cerrar sesion(Default '')

此服务用于管理视图,如果使用 render 方法,则进行渲染;如果使用 json,则解析响应。

注意 当使用 json 时,仅适用于 EasyListEasyShow,并且不能将两个或多个视图分组。

EasyForm

创建一个包含表单的视图,表单必须事先创建。

$form = $this->createForm(MascotaType::class, new Mascota())->createView();
$easyForm = EasyForm::easy('Agregar Masctota', $form);

要向表单添加更多选项

$easyForm->addLinkBack($route, $parametros, $titulo ) ('btn-secondary', 'fa-arrow-left')
$easyForm->addLinkShow($route, $parametros, $titulo) ('btn-success', 'fa-list-ul')
$easyForm->addLink($route, $parametros, $titulo, $clase = 'btn-secondary', $fa_icon = null)
$easyForm->setDeleteForm($form_delete)
$easyForm->cleanLinks()

EasyPanel 服务负责将信息发送到模板,而 twig 负责渲染

return $easypanel->render($easyForm);

要配置表单,需要转到 Type 文件并执行必要的验证和配置

EasyList

此类直接依赖于插件 EasyDoctrine 和类 EasyData

此类允许生成一个查询的表(列表)并显示指定的字段。添加分页、搜索、排序和项目选择。

$easyList = EasyForm::easy('Lista de mascotas', EasyData $easyData, ['id','nombre','creado']);
$easyList->setLabelsTable(['ID','Mascota','Registrado']);

setLabelsTable 只对 html 视图有效

可以通过服务 $easypanel 在 json 和 html 之间切换响应

HTML

return $easypanel->render($easyList);

API

return $easypanel->json($easyList);

EasyShow

允许生成一个显示对象详细信息的表格

$easyShow = EasyForm::easy('Ver Mascota', $objeto, ['id','nombre','raza','propietario']);
$easyShow->setLabelsFields(['ID','Nombre de la Mascota','Raza','Propietario']);

setLabelsFields 仅对 html 视图有效

与 EasyList 类似,可以通过 $easypanel 服务在 json 和 html 之间切换

HTML

return $easypanel->render($easyShow);

API

return $easypanel->json($easyShow);

EasyShow 和 EasyList 响应配置

为了能够灵活创建视图和 json 响应,这两个类允许使用多个方法

渲染为

允许修改查询得到的字段值并按不同格式显示的方法

jsonrender 方法中有效

$list->renderAsText('nombre'); <p>Popi</p> Default para todos los campos
$list->renderAsImage('foto','uploads/images'); domain.com/asssets/uploads/images/perro.jpg
$list->renderAsBoolean('activo'); <i class="fa fa-check"></i> <i class="fa fa-times"></i>
$list->renderAsDate('nacimiento'); 2020-01-01
$list->renderAsTime('consulta_hora'); 12:00:00
$list->renderAsDateTime('creacion'); 2021-01-01 09:00:00
$list->renderAsRaw('descripcion'); <p>Parrafo generado con algun editor web</p>
$list->renderAsJson('Etiquetas'); <ul><li>Pequeña</li><li>Cachorro</li></ul>
$list->renderAsLink('certificado','uploads/docs'); domain.com/asssets/uploads/docs/CErtificado.pdf
$list->renderAsTranslate('cv'); (detalle acontinuacion)

renderAsImagerenderAsLink

renderAsImage 和 renderAsLink 方法允许添加一个路径,以正确显示图像或文件。

如果提供的路径中已经包含文件名,则使用该文件名,如果没有包含,则使用 $path.'/'.$valor 构建路径

如果忽略,则仅添加 '/' 来标记站点的根目录

$manager = $this->get('assets.packages');
$manager->getUrl('comprobantes'));

...
$vista->renderAsImage('fotoperfil',$manager->getUrl('uploads/perfil'))
...

假设 fotoperfil 方法返回的值为 fotousuario.jpg,则生成的 html 结果为

$vista->renderAsImage('fotoperfil',$manager->getUrl('uploads/perfil'))
...
<img src="/uploads/perfil/fotousuario.jpg" alt="Image" class="img-responsive easypanel-img">

对于 renderAslink 方法,建议使用 symfony 的 generateUrl 方法

假设 archivo 方法返回的值为 registro1.pdf,并以 file_preview 为示例路径,配置为 /vista/{archivo}/preview,则结果为

$vista->renderAsLink('fotoperfil',$this->generateUrl('file_preview',['archivo'=>$objeto->getArchivo()]))
...
<a href="/vista/registro1.pdf/preview" target="_blank"  class="img-responsive easypanel-link">registro1.pdf</a>

如果生成的路径中不包含文件名,则仅将其添加到末尾

$vista->renderAsLink('fotoperfil','/vista-archivo/preview')
...
<a href="/vista-archivo/preview/registro1.pdf" target="_blank"  class="img-responsive easypanel-link">registro1.pdf</a>

翻译

为了应用翻译,该包基于 knplabs/doctrine-behaviors 的配置

https://github.com/KnpLabs/DoctrineBehaviors

为了应用它,只需配置列即可

[...,'translate.titulo',...]

默认情况下,翻译将使用 Symfony 的 Request 类的 locale 参数

为了能够同时查看多个翻译,请将语言添加到列中

[...,'translate.titulo~en|es',...]
[...,'translate.titulo~en|es|it',...]

renderAsTranslate

最后,为了能够显示翻译,我们创建了 renderAsTranslate 方法

$view->renderAsTranslate('translate.titulo');
$view->renderAsTranslate('translate.titulo~en|es');
$view->renderAsTranslate('translate.titulo~en|es|it');

关系

如果有相关查询,可以选择要显示相关对象的哪个方法

$columnas=['userid', 'username','usertask.name'];

方法 useridusername 将分别打印 usertask 的对应值,内部会检查关系是否返回了 Null 值,然后进行调用。内部的执行流程如下

$relacion = $objeto->getUsertask()
if($relacion!=null){
    return $relacion->getName();
}else{
    return '';
}

在版本 3 中,允许最多 2 级关系

$columnas=['userid', 'username','localidad.municipio.nombre'];
$usuario->getLocalidad()->getMunicipio()->getNombre()

EasyList 配置

为了增强列表视图的可用性,我们有不同的方法

路由

必须使用 symfony 在项目中注册的路由

$list->tableLinkEdit($route, $parametros, $nombre)
$list->tableLinkShow($route, $parametros, $nombre)
$list->tableLink($route, $parametros, $texto, $clase = 'btn-secondary', $fa_icon = null)
$list->tableCleanLinks()

排序和搜索

$list->enableSearch($value,$textbutton = 'fa-search',$classbutton='btn',$classcontainer='')
$list->enableOrder($route,$parametersroute,$ordercolumn = 1,$ordertype = 'ASC');

分页

$list->setPageInfo($pageinfo);
$list->createListPages($search,$route,$classitem,$classactive,$first = "",$last = "");

行计数器

    $list->setFirstColumnCount(true);

注意 仅当部分显示为 html 时才有效

全局:视图中末尾的链接

在三种类型的类中,都允许在部分末尾添加链接

该路由必须是 symfony 在项目中定义的路由

$list->addLinkEdit( $route, $parametros, $nombre)   ('btn-info', 'fa-edit')
$list->addLinkBack( $route, $parametros,$nombre )   ('btn-secondary', 'fa-arrow-left')
$list->addLink($route, $parametros, $texto,$clase = 'btn-secondary',$fa_icon = null)
$list->cleanLinks()

注意 仅当部分显示为 html 时才有效

隐藏 symfony

在 services.yml 文件中将 CustomEnconder 类添加为服务,并将该服务与 security.yml 文件中的 encoder 关联的表目标关联

(services.yml)
    app.my_custom_encoder:
        class: Ast\EasyPanelBundle\CustomPassword\CustomEnconder


(security.yml)
    security:
        encoders:
          AppBundle\Entity\User:
              id: app.my_custom_encoder


BuildPassword 类

如果需要创建密码且无法访问 encoder,则 BuildPassword 类可以生成与 encoder 相同结果的密码

$bp = new BuildPassword()
$bp->generate($pass,BuildPassword::randomSalt());
$hashpass = $bp->getPass();
$salt = $bp->getSalt();

BuildPassword::create($pass,$salt)



### Authors

* **Antonio Samano** - *Initial work* - [Antoniosam](https://github.com/antoniosam)

### License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details