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 Generator Laravel Package

aleste/crud-generator

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require aleste/crud-generator
    

    Register the bundle in config/bundles.php:

    return [
        // ...
        Aleste\CrudGeneratorBundle\AlesteCrudGeneratorBundle::class => ['all' => true],
    ];
    
  2. Generate a CRUD: Use the console command to scaffold a CRUD for an existing entity (e.g., Post):

    php bin/console generate:crud AlesteCrudGeneratorBundle:Crud Post
    

    This creates:

    • Controller (src/Controller/PostCrudController.php)
    • Twig templates (templates/crud/post/)
    • Routes (config/routes/crud.yaml)
  3. First Use Case: Access the generated CRUD at /post/crud. The bundle provides:

    • List view with pagination (via KnpPaginatorBundle).
    • Filterable fields (via LexikFormFilterBundle).
    • Export buttons (CSV/Excel) for the list view.
    • Standard CRUD operations (create, read, update, delete).

Implementation Patterns

Workflow Integration

  1. Entity-Centric Generation:

    • Generate CRUDs for any Doctrine entity with annotations or YAML/XML mappings.
    • Customize fields via CrudGeneratorBundle's configuration in config/packages/aleste_crud_generator.yaml:
      aleste_crud_generator:
          cruds:
              Post:
                  fields:
                      - { property: 'title', type: 'text' }
                      - { property: 'content', type: 'textarea' }
                  filters:
                      - { property: 'published', type: 'boolean' }
      
  2. Template Overrides:

    • Override default Twig templates by copying files from vendor/aleste/crud-generator-bundle/Resources/views/crud/ to templates/crud/.
    • Example: Customize the list template at templates/crud/post/index.html.twig.
  3. Dynamic Routing:

    • Routes are auto-generated in config/routes/crud.yaml. Extend them by adding custom routes:
      post_crud_custom:
          path: /post/custom
          controller: App\Controller\PostCrudController::customAction
      
  4. Form Customization:

    • Extend generated forms by creating a custom form type and referencing it in the controller:
      // src/Form/PostType.php
      class PostType extends AbstractType {
          public function buildForm(FormBuilderInterface $builder, array $options) {
              $builder->add('customField', TextType::class);
          }
      }
      
    • Update the controller to use the custom form:
      $this->formFactory->createNamedBuilder('post', PostType::class, $post);
      
  5. Event Listeners:

    • Hook into CRUD lifecycle events (e.g., CrudGenerateEvent) to modify generation behavior:
      // src/EventListener/CrudGenerateListener.php
      class CrudGenerateListener implements EventSubscriberInterface {
          public static function getSubscribedEvents() {
              return [
                  CrudEvents::PRE_GENERATE => 'onPreGenerate',
              ];
          }
          public function onPreGenerate(CrudGenerateEvent $event) {
              $event->setTemplatePath('custom/path');
          }
      }
      
  6. API Integration:

    • Combine with API Platform or FOSRestBundle to expose CRUD endpoints. Example:
      # config/routes/api.yaml
      app_post_crud_api:
          path: /api/posts
          controller: App\Controller\PostCrudController::apiListAction
          methods: [GET]
      

Gotchas and Tips

Common Pitfalls

  1. Bundle Compatibility:

    • The package targets Symfony 2.3–2.4 (deprecated). For Symfony 4/5/6, consider forking or using alternatives like EasyAdminBundle.
    • Dependencies like sensio/generator-bundle are outdated. Manually generate controllers if issues arise.
  2. Template Caching:

    • Clear cache after template overrides:
      php bin/console cache:clear
      
  3. Filter Bundle Conflicts:

    • LexikFormFilterBundle may conflict with other filter bundles. Exclude unused filters in config/packages/lexik_form_filter.yaml:
      lexik_form_filter:
          filter_form_types:
              - App\Form\Type\PostFilterType
          ignored_form_types:
              - Symfony\Component\Form\Extension\Core\Type\FormType
      
  4. Entity Changes:

    • Regenerate CRUD after schema changes:
      php bin/console generate:crud AlesteCrudGeneratorBundle:Crud Post --force
      
  5. Pagination Issues:

    • Ensure knp_paginator is configured in config/packages/knp_paginator.yaml:
      knp_paginator:
          page_range: 5
          default_options:
              page_name: page
              sort_field_name: sort
              sort_direction_name: direction
              distinct: true
      
  6. Export Problems:

    • Dependencies like phpoffice/phpexcel may be missing. Install manually:
      composer require phpoffice/phpexcel
      

Debugging Tips

  1. Check Generated Files:

    • Inspect var/cache/dev/ for compiled templates and routes.
    • Verify src/Controller/ for generated controllers.
  2. Enable Debug Mode:

    # config/packages/dev/debug.yaml
    framework:
        profiler: { only_exceptions: false }
    
  3. Log Events:

    • Enable event logging in config/packages/monolog.yaml:
      monolog:
          handlers:
              crud:
                  type: stream
                  path: "%kernel.logs_dir%/crud.log"
                  level: debug
      

Extension Points

  1. Custom Actions:

    • Add custom actions to the controller:
      public function bulkAction(Request $request) {
          $ids = $request->request->get('ids');
          // Custom logic (e.g., bulk delete)
          return $this->redirectToRoute('post_crud_index');
      }
      
    • Update the template to include a bulk action button:
      {{ form_start(form.bulk_action) }}
          <button type="submit">Bulk Action</button>
      {{ form_end(form.bulk_action) }}
      
  2. Dynamic Field Mapping:

    • Use callbacks in aleste_crud_generator.yaml:
      fields:
          - { property: 'createdAt', type: 'datetime', options: { callback: 'App\Callback\FormatDate' } }
      
  3. Security:

    • Add custom security checks in the controller:
      public function indexAction() {
          $this->denyAccessUnlessGranted('ROLE_POST_MANAGER');
          // ...
      }
      
  4. Internationalization:

    • Translate labels/placeholders in translations/messages.en.yaml:
      'post.crud.title': 'Post Title'
      'post.crud.actions.edit': 'Edit Post'
      
  5. Testing:

    • Test generated CRUDs with functional tests:
      public function testCrudGeneration() {
          $client = static::createClient();
          $crawler = $client->request('GET', '/post/crud');
          $this->assertSelectorTextContains('h1', 'Posts');
      }
      
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui