Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Crud Admin Bundle Laravel Package

dontdrinkandroot/crud-admin-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require dontdrinkandroot/crud-admin-bundle
    

    Add to config/bundles.php:

    return [
        // ...
        Dontdrinkandroot\CrudAdminBundle\DontdrinkandrootCrudAdminBundle::class => ['all' => true],
    ];
    
  2. Basic Configuration Create a YAML config file (e.g., config/crud_admin.yaml) with a minimal entity definition:

    resources:
        App\Entity\Product:
            fields: [id, name, price]
            actions: [new, edit, delete]
    
  3. First Use Case Run migrations (if needed) and access the admin panel at /admin. The bundle auto-generates CRUD interfaces for configured entities.


Where to Look First

  • Configuration: config/crud_admin.yaml (primary config file).
  • Entity Annotations: Check src/Entity/ for annotated properties (e.g., @ORM\Column).
  • Templates: Override default Twig templates in templates/crud_admin/.
  • Doctrine Integration: Ensure your entities are properly mapped (e.g., App\Entity\Product extends Doctrine\ORM\Mapping\Entity).

Implementation Patterns

Core Workflows

  1. Entity Configuration Define CRUD behavior per entity in crud_admin.yaml:

    resources:
        App\Entity\User:
            fields: [username, email, roles]  # Columns to display
            searchable: [username, email]    # Search fields
            sortable: [username, createdAt]  # Sortable columns
            actions: [new, edit, delete, show]  # Available actions
            filters: [active]                # Custom filters
    
  2. Field Customization Use annotations or YAML to customize fields:

    fields:
        name:
            label: "Product Name"
            type: "text"
            options:
                attr:
                    placeholder: "Enter product name"
    
  3. Action Overrides Extend default actions (e.g., edit) via event listeners or custom controllers:

    // src/EventListener/CrudAdminListener.php
    use Dontdrinkandroot\CrudAdminBundle\Event\CrudAdminEvent;
    
    public function onPreEdit(CrudAdminEvent $event) {
        $entity = $event->getEntity();
        // Custom logic before edit
    }
    
  4. Integration with Forms Use Symfony’s form system for complex validation:

    form:
        type: App\Form\ProductType
        options:
            validation_groups: [Default, product]
    

Advanced Patterns

  1. Dynamic Routing Generate routes dynamically via annotations:

    // src/Entity/Product.php
    use Dontdrinkandroot\CrudAdminBundle\Annotation\CrudAdmin;
    
    /**
     * @CrudAdmin\Route(
     *     name="product_custom_route",
     *     path="/custom-products"
     * )
     */
    class Product {}
    
  2. Batch Actions Enable bulk operations in YAML:

    actions:
        batch: [publish, archive]
    
  3. Access Control Restrict access via security voters or annotations:

    security:
        roles: [ROLE_ADMIN, ROLE_SUPER_ADMIN]
    
  4. API Integration Expose CRUD endpoints via API Platform or custom controllers:

    api:
        enabled: true
        prefix: /api/products
    

Gotchas and Tips

Common Pitfalls

  1. Entity Mapping Issues

    • Symptom: Blank admin panel or errors like No entity found.
    • Fix: Ensure entities are properly mapped with @ORM\Entity and use Doctrine\ORM\Mapping as ORM.
    • Debug: Run php bin/console doctrine:schema:validate to check mappings.
  2. Caching Problems

    • Symptom: Changes to YAML/config not reflecting.
    • Fix: Clear cache:
      php bin/console cache:clear
      
  3. Field Type Mismatches

    • Symptom: Fields not rendering correctly (e.g., dates as text).
    • Fix: Explicitly define field types in YAML:
      fields:
          createdAt:
              type: "datetime"
              format: "yyyy-MM-dd HH:mm"
      
  4. Event Listener Conflicts

    • Symptom: Custom listeners not triggering.
    • Fix: Ensure listeners are tagged correctly:
      // src/EventListener/CrudAdminListener.php
      public static function getSubscribedEvents() {
          return [
              CrudAdminEvents::PRE_EDIT => 'onPreEdit',
          ];
      }
      

Debugging Tips

  1. Enable Debug Mode Set APP_DEBUG=true in .env to see detailed errors and logs.

  2. Log Events Use Symfony’s event dispatcher to log CRUD actions:

    public function onPostSave(CrudAdminEvent $event) {
        \Log::info('Entity saved:', [$event->getEntity()->getId()]);
    }
    
  3. Check Twig Templates Override templates to debug rendering:

    {# templates/crud_admin/base.html.twig #}
    <div class="debug">{{ dump(app) }}</div>
    

Extension Points

  1. Custom Field Types Create a custom field type by extending Dontdrinkandroot\CrudAdminBundle\Form\Type\FieldType:

    namespace App\Form\Type;
    
    use Dontdrinkandroot\CrudAdminBundle\Form\Type\FieldType;
    
    class CustomFieldType extends FieldType {
        public function configureOptions(OptionsResolver $resolver) {
            $resolver->setDefaults([
                'type' => 'text',
                'attr' => ['class' => 'custom-field'],
            ]);
        }
    }
    

    Register in services:

    # config/services.yaml
    App\Form\Type\CustomFieldType: ~
    
  2. Dynamic Field Loading Use callbacks to load fields dynamically:

    fields:
        dynamicField:
            type: "callback"
            callback: "App\Service\DynamicFieldLoader::load"
    
  3. Custom Actions Create a custom action by extending Dontdrinkandroot\CrudAdminBundle\Action\Action:

    namespace App\Action;
    
    use Dontdrinkandroot\CrudAdminBundle\Action\Action;
    
    class CustomAction extends Action {
        public function execute() {
            // Custom logic
            return $this->redirectToRoute('admin_product_index');
        }
    }
    

    Register in YAML:

    actions:
        custom: App\Action\CustomAction
    

Configuration Quirks

  1. Priority Order

    • YAML config overrides annotations.
    • Event listeners fire in the order they are registered.
  2. Doctrine vs. Custom Repositories The bundle defaults to Doctrine’s EntityRepository. For custom repos:

    repository:
        class: App\Repository\CustomProductRepository
    
  3. Translation Use Symfony’s translation system for labels:

    fields:
        name:
            label: "crud_admin.product.name"
    

    Define translations in translations/messages.en.yaml:

    crud_admin:
        product:
            name: "Product Name (Translated)"
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle