EasyAdminBundle Documentation

repository·5.x·Indexed 26 days ago

https://github.com/easycorp/easyadminbundle

A tool for building responsive and customizable admin panels for Symfony applications using PHP. It requires PHP 8.2+ and Symfony 6.4+ for the latest stable version (5.x). The bundle provides features for generating Dashboards and CRUD controllers, configuring built-in and custom actions, managing batch actions via BatchActionDto, and integrating standard Symfony controllers using the #[AdminRoute] attribute.

Tokens
65.7K
Snippets
180
Records
309
Agent score
87%

What's inside EasyAdminBundle

  1. Use the MoneyField in EasyAdmin

    5.x

    The MoneyField is used to represent properties that store monetary amounts. It renders as an <input type="number"> in forms and handles formatting in read-only pages like index and detail views.

    Technical Details:

    • PHP Class: EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField
    • Symfony Form Type: MoneyType
    • Database Types: Works with decimal, float, or integer.
  2. Use CodeEditorField for source code editing

    5.x

    The CodeEditorField provides a JavaScript-based editor powered by CodeMirror for reading and writing source code in EasyAdmin CRUD pages. It uses a text Doctrine DBAL type and renders as a <textarea> that is transformed into a code editor via JavaScript.

    Use this field when you need syntax highlighting. For long text without formatting, use TextAreaField. For rich text formatting, use TextEditorField.

  3. Understand CRUD controller pages and actions

    5.x

    CRUD controllers manage four main pages, each corresponding to a specific action in AbstractCrudController:

    • index: Displays a paginated, sortable list of entities with search and filters.
    • detail: Displays the contents of a specific entity.
    • new: Allows creating new entity instances.
    • edit: Allows updating properties of an existing entity.

    Secondary actions like delete and autocomplete are also available but do not correspond to a dedicated main page.

  4. Use AssociationField to link Doctrine entities

    5.x

    The AssociationField displays properties used to associate Doctrine entities (one-to-one, one-to-many, etc.).

    • In form pages (edit/new): It renders an advanced autocomplete widget based on the TomSelect library.
    • In read-only pages (index/detail): It displays a clickable link pointing to the detail action of the related entity.
    • Nested associations: You can use dot syntax to access nested properties, e.g., AssociationField::new('author.publisher').

    Technical Details:

    • PHP Class: EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField
    • Symfony Form Type: EntityType
  5. Use TextField for short text content

    5.x
    Use the TextField class to represent short text content in your EasyAdmin CRUD. For longer text, consider using TextareaField, TextEditorField, or CodeEditorField. For specialized data like emails, telephone numbers, or URLs, use the specific field types provided by EasyAdmin (e.g., EmailField, TelephoneField, UrlField).
  6. Use ImageField to manage image uploads

    5.x

    The EasyCorp\Bundle\EasyAdminBundle\Field\ImageField is used to manage image uploads in the backend. The entity property stores only the relative path to the image, while the actual file is stored on the server filesystem or a remote system via league/flysystem-bundle.

    Key Details:

    • PHP Class: EasyCorp\Bundle\EasyAdminBundle\Field\ImageField
    • Doctrine DBAL Type: string
    • Symfony Form Type: FileUploadType (custom EasyAdmin type)
    • HTML Rendering: Renders as a dynamic <input type="file"> widget via JavaScript.
  7. Create custom pages using the Content Page Template

    5.x

    To create a custom page that maintains the EasyAdmin layout (header, sidebar, and content area), extend the @EasyAdmin/page/content.html.twig template. You can override blocks like content_title, page_actions, and main to define your own content.

    {# templates/admin/my-custom-page.html.twig #}
    {% extends '@EasyAdmin/page/content.html.twig' %}
    
    {% block content_title %}The Title of the Page{% endblock %}
    
    {% block page_actions %}
        <a class="btn btn-primary" href="...">Some Action</a>
    {% endblock %}
    
    {% block main %}
        <table class="datagrid">
            <thead class="datagrid-header">
                <tr>
                    <th>Some Column</th>
                    <th>Another Column</th>
                </tr>
            </thead>
            <tbody>
                {% for data in my_own_data %}
                    <tr>
                        <td>{{ data.someColumn }}</td>
                        <td>{{ data.anotherColumn }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% endblock %}
  8. Configure Content Security Policy (CSP) support

    5.x

    EasyAdmin supports Content Security Policy (CSP) nonces to prevent XSS attacks. To enable this:

    1. Install and configure NelmioSecurityBundle in your Symfony application.
    2. EasyAdmin will automatically detect the csp_nonce() Twig function and apply the nonce attribute to all its internal script tags.
  9. List and troubleshoot EasyAdmin routes

    5.x

    EasyAdmin generates one route for each CRUD action of each dashboard. You can list all available admin routes using the Symfony CLI. If routes are missing, clear the Symfony cache to trigger the EasyAdmin route loader.

    To list routes:

    php bin/console debug:router

    To clear cache:

    php bin/console cache:clear
  10. Migrate Main Menu links in EasyAdmin 5.x

    5.x

    The linkToCrud() method is removed. Use linkTo() and pass the Controller class directly.

    // Before (4.x)
    yield MenuItem::linkToCrud('Categories', 'fa fa-tags', Category::class);
    
    // After (5.x)
    yield MenuItem::linkTo(CategoryCrudController::class, 'Categories', 'fa fa-tags');