cleverage/admin-bundle

具有内置路由生成的简单可扩展基于动作的行政管理系统

v3.1 2020-11-20 14:47 UTC

README

控制器、路由组件、表单和实体之间的缺失链接。

Twig 示例

<a href="{{ admin_path('post', 'list') }}">List</a>
<a href="{{ entity_path(entity, 'edit') }}">Edit</a>

警告

此捆绑包需要 Symfony 的安全组件,它将强制使用投票器访问任何实体。为了简化配置,我们建议使用 cleverage/permission-bundle。此外,由于设计错误,此捆绑包与 Doctrine 的使用紧密相关,虽然它可以用于非 Doctrine 实体,但要从依赖项中取消 Doctrine 将需要一些额外的工作。

配置

简单示例

sidus_admin:
    configurations:
        post:
            entity: MyBundle\Entity\Post # Class name of your entity
            prefix: /post # Routing prefix
            controller_pattern:
                - 'Sidus\AdminBundle\Action\{{Action}}Action' # Full controller reference
            template_pattern:
                - '@SidusAdmin/Action/{{action}}.{{format}}.twig'
            actions:
                list:
                    path:     /list # Routing path
                edit:
                    path:     /edit/{id}
                    form_type: MyBundle\Form\Type\EditPostType # Form type to use in controller

配置参考

除非指定其他值,否则所有值都是默认值。

sidus_admin:
    admin_class: Sidus\AdminBundle\Admin\Admin
    action_class: Sidus\AdminBundle\Admin\Action
    configurations:
        <admin_code>:
            entity: ~ # REQUIRED: The fully qualified class name of the entity (or the Doctrine's shorter reference)
            prefix: ~ # REQUIRED: Routing prefix for all actions
            controller_pattern: [] # The controller reference that will be used to generate routing
            # Available interpolation variables are:
            # {{admin}} lowercase first letter admin code
            # {{Admin}} uppercase first letter admin code
            # {{action}} lowercase first letter action code
            # {{Action}} uppercase first letter action code
            # If you don't set any controller_pattern you will need to set the _controller attribute in the defaults of
            # each action.
            template_pattern: [] # The template pattern
            action_class: # Defaults to main action_class
            options: {} # You can put anything here
            actions:
                <action_code>: # The action code needs to match the controller's method name without the "Action" suffix
                    form_type: ~ # Useful in combination with AbstractAdminController::getForm($request, $data)
                    form_options: ~ # Static form options
                    template: ~ # Computed by the TemplateResolver using template_pattern if null
                    # All the following options are used to generate the route for the routing component
                    # See Symfony doc here: https://symfony.ac.cn/doc/current/routing.html
                    path: ~ # REQUIRED
                    defaults: ~
                    requirements: ~
                    options: ~
                    host: ~
                    schemes: ~
                    methods: ~
                    condition: ~

使用方法

生成路由

当路由到实体时,AdminRouter 组件将尝试从路由上下文中获取缺失的路由参数,然后从实体本身获取,这意味着如果您将路由参数名称命名为与您的实体属性一致,则无需手动传递任何参数。

PHP

<?php
/** @var $adminRouter AdminRouter */
use Sidus\AdminBundle\Routing\AdminRouter;$adminRouter->generateAdminPath('post', 'list');
$adminRouter->generateEntityPath($entity, 'edit');

当处理单个类的多个管理员时,您可以使用此函数代替

<?php
/** @var $adminRouter AdminRouter */
use Sidus\AdminBundle\Routing\AdminRouter;$adminRouter->generateAdminEntityPath('post', $entity, 'edit');

Twig

<a href="{{ admin_path('post', 'list') }}">List</a>
<a href="{{ entity_path(entity, 'edit') }}">Edit</a>

当处理单个类的多个管理员时,您可以使用此函数代替

<a href="{{ admin_entity_path('post', entity, 'edit') }}">Edit</a>

附加可选参数

对于每个方法,您可以在动作名称之后的参数中传递额外的路由参数,您还可以设置 URL 生成器接口的引用类型(绝对、相对等)。

<?php
/** @var $adminRouter AdminRouter */
use Sidus\AdminBundle\Routing\AdminRouter;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;$adminRouter->generateAdminEntityPath(
    'post',
    $entity,
    'edit',
    ['parametrer' => 'value'],
    UrlGeneratorInterface::ABSOLUTE_PATH
);