2lenet/crudit-bundle
Symfony CruditBundle for building CRUD back offices with SB Admin layout. Provides configurable controllers, datasources and filtersets, plus list views with pagination/sorting, actions, grouping, batch ops, export (CSV/Excel), markdown, workflows and more.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require 2lenet/crudit-bundle
npm install bootstrap@5 sass sass-loader @fortawesome/fontawesome-free easymde --save
Enable the Bundle:
Add to config/bundles.php:
return [
// ...
Lle\CruditBundle\LleCruditBundle::class => ['all' => true],
];
Basic Twig Integration:
Extend the provided layout in your base template (templates/base.html.twig):
{% extends '@LleCrudit/layout/sb_admin/layout.html.twig' %}
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{% endblock %}
{% block javascripts %}
{{ parent() }}
{{ encore_entry_script_tags('app') }}
{% endblock %}
First CRUD:
Follow the CRUD doc to scaffold a basic CRUD interface for an entity (e.g., App\Entity\Post):
php bin/console lle:crudit:generate Post
Generate a CRUD for a Product entity and customize the list view to display name, price, and createdAt:
php bin/console lle:crudit:generate Product
Then override the list template in templates/LleCrudit/Product/list.html.twig:
{% extends '@LleCrudit/Product/list.html.twig' %}
{% block table_headers %}
{{ parent() }}
<th>Price</th>
{% endblock %}
{% block table_rows %}
{{ parent() }}
<td>{{ product.price }}</td>
{% endblock %}
Scaffolding:
Use the lle:crudit:generate command to create CRUD interfaces for entities:
php bin/console lle:crudit:generate User --fields=name,email,roles --list=name,email,createdAt
--fields: Specify fields to include in the form.--list: Define columns for the list view.Conditional UI Elements: New in 1.18.4: Hide bricks, tabs, or actions dynamically based on the request context (e.g., user role, request type). Configure in YAML:
# config/packages/lle_crudit.yaml
lle_crudit:
cruds:
App\Entity\Product:
bricks:
- { name: 'product_details', hidden: 'request.get('_route') == "api_products_show"' }
tabs:
- { name: 'advanced', hidden: 'not user.hasRole("ROLE_ADMIN")' }
actions:
- { name: 'delete', hidden: 'not is_granted("DELETE", product)' }
Customization:
Override default templates in templates/LleCrudit/[EntityName]/:
list.html.twig: Modify the table structure.edit.html.twig: Customize the form fields.show.html.twig: Adjust the detail view.Field Types:
Leverage built-in field types (e.g., text, number, boolean, association) or create custom ones by extending Lle\CruditBundle\Form\Type\AbstractType.
Filters:
Add filters to list views using the filters option in the CRUD configuration (see Filters doc):
# config/packages/lle_crudit.yaml
lle_crudit:
cruds:
App\Entity\Product:
list:
filters:
- { name: 'name', type: 'text' }
- { name: 'price', type: 'number' }
Menus:
Integrate CRUDs into the sidebar menu via the menu configuration:
lle_crudit:
menu:
- { route: 'lle_crudit_product_list', label: 'Products', icon: 'box' }
Bricks: Reuse UI components (e.g., cards, modals) and conditionally hide them:
{% render_brick 'product_card', { product: product, hidden: 'not user.hasRole("ROLE_EDITOR")' } %}
Webpack Encore:
Ensure app.js imports Crudit’s SCSS after your custom styles:
// assets/js/app.js
import '../styles/app.scss';
// assets/styles/app.scss
@import '../../vendor/le/crudit-bundle/assets/sb-admin/css/app.scss';
Security:
Protect CRUD routes with Symfony’s security system (e.g., @IsGranted("ROLE_ADMIN") in controllers) or use the new hidden configuration for dynamic visibility.
Validation: Extend entity validation or use Crudit’s built-in validation by configuring field rules in the CRUD YAML:
lle_crudit:
cruds:
App\Entity\Product:
fields:
price:
validation: { notBlank: true, min: 0 }
API Integration:
Use Crudit for admin interfaces while keeping API endpoints separate (e.g., via Symfony’s ApiPlatform or custom controllers). Leverage the hidden feature to exclude UI elements from API requests:
lle_crudit:
cruds:
App\Entity\Product:
bricks:
- { name: 'admin_actions', hidden: 'request.isXmlHttpRequest()' }
SCSS Order:
app.scss (as shown in the README).Webpack Encore Conflicts:
webpack.config.js to resolve dependencies.Template Overrides:
{% block %} inheritance carefully. For example, to add a column:
{% block table_rows %}
{{ parent() }} {# Renders default rows #}
<td>{{ product.customField }}</td>
{% endblock %}
Entity Changes:
templates/LleCrudit/[EntityName]/ directory and regenerate:
rm -rf templates/LleCrudit/Product/
php bin/console lle:crudit:generate Product
Performance:
limit option in the CRUD config:
lle_crudit:
cruds:
App\Entity\Product:
list:
pagination: 20
Conditional Logic Errors:
hidden expressions in YAML may cause runtime errors.{% if request.get('_route') == "api_products_show" %}) before applying them to YAML.Console Commands: List available CRUDs to verify configuration:
php bin/console lle:crudit:list
Template Debugging:
Enable Twig debug mode in .env:
APP_DEBUG=1
Use {{ dump(_context) }} in templates to inspect variables.
JavaScript Errors: Check the browser console for errors related to:
$primary).app.scss:
$primary: #your-color !default;
@import '../../vendor/le/crudit-bundle/assets/sb-admin/css/app.scss';
Conditional UI Debugging:
{% if not brick.hidden %}
{% render_brick 'product_card', { product: product } %}
{% else %}
{# Debug: {{ dump(brick.hidden) }} #}
{% endif %}
AbstractType:
// src/Form/Type/CustomFieldType.php
namespace App\Form\Type;
use Lle\CruditBundle\Form\Type\AbstractType;
class CustomFieldType extends AbstractType {
public function build
How can I help you explore Laravel packages today?