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

Action Bundle Laravel Package

colin/action-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require maximecolin/colin-action-bundle
    

    Add to config/bundles.php:

    return [
        // ...
        MaximeColin\Bundle\ActionBundle\ColinActionBundle::class => ['all' => true],
    ];
    
  2. Basic Configuration Define actions in config/packages/colin_action.yaml:

    colin_action:
        configurations:
            list:   Colin\Bundle\ActionBundle\Configuration\ListActionConfiguration
            create: Colin\Bundle\ActionBundle\Configuration\CreateActionConfiguration
            update: Colin\Bundle\ActionBundle\Configuration\UpdateActionConfiguration
            delete: Colin\Bundle\ActionBundle\Configuration\DeleteActionConfiguration
            read:   Colin\Bundle\ActionBundle\Configuration\ReadActionConfiguration
        actions:
            admin:
                dummy:
                    create:
                        type: create
                        configs:
                            entity_class: App\Entity\Dummy
                            form_type: App\Form\DummyType
                            template: App:Dummy:create.html.twig
    
  3. First Use Case Generate routes by dumping them:

    php bin/console debug:router | grep dummy_create
    

    Access the action via the generated route (e.g., /admin/dummy/create).


Implementation Patterns

Workflow: CRUD Actions

  1. Define Configuration Extend default configurations in config/packages/colin_action.yaml for custom behavior (e.g., add validation rules, change templates).

  2. Entity & Form Setup Ensure your entity (App\Entity\Dummy) and form (App\Form\DummyType) exist and are referenced in the config.

  3. Template Overrides Place custom templates in templates/App/Dummy/ (e.g., create.html.twig). Use Twig variables like {{ form_widget(form) }} and {{ action }} for context.

  4. Routing Routes are auto-generated under the admin namespace. Override paths in config if needed:

    configs:
        path: /custom/dummy/create
    
  5. Accessing Actions Use the router or generate URLs programmatically:

    $url = $this->generateUrl('admin_dummy_create');
    

Workflow: Custom Actions

  1. Create a New Action Class Extend Colin\Bundle\ActionBundle\Action\AbstractAction:

    namespace App\Action;
    use Colin\Bundle\ActionBundle\Action\AbstractAction;
    
    class ExportAction extends AbstractAction {
        public function execute() {
            // Custom logic
            return $this->render('App:Dummy:export.html.twig');
        }
    }
    
  2. Register the Action Add to config/packages/colin_action.yaml:

    actions:
        admin:
            dummy:
                export:
                    type: export
                    configs:
                        entity_class: App\Entity\Dummy
                        action_class: App\Action\ExportAction
    
  3. Configure Dependencies Inject services into your action class via constructor (use ContainerAwareTrait if needed).


Integration Tips

  • Security: Use Symfony’s security system to protect routes:
    # config/routes.yaml
    admin_dummy_create:
        path: /admin/dummy/create
        controller: colin_action.controller
        methods: [GET, POST]
        defaults:
            _action: admin.dummy.create
        requirements:
            _role: ROLE_ADMIN
    
  • Event Listeners: Attach listeners to actions via Symfony’s event dispatcher:
    $eventDispatcher->addListener('action.pre.execute', function (ActionEvent $event) {
        if ($event->getActionName() === 'admin.dummy.create') {
            // Pre-execution logic
        }
    });
    
  • Testing: Mock actions in PHPUnit:
    $action = $this->createMock(\App\Action\ExportAction::class);
    $this->container->set('app.action.export', $action);
    

Gotchas and Tips

Pitfalls

  1. Entity Class Not Found

    • Issue: entity_class in config points to a non-existent class.
    • Fix: Verify the FQCN (e.g., App\Entity\Dummy) and namespace autoloading.
  2. Form Type Mismatch

    • Issue: form_type references a form that doesn’t exist or isn’t registered as a type.
    • Fix: Ensure the form is tagged as a type service:
      // src/Form/DummyType.php
      class DummyType extends AbstractType {
          public function buildForm(FormBuilderInterface $builder, array $options) {
              // ...
          }
      }
      
      And is autowired or manually registered.
  3. Template Not Found

    • Issue: Twig throws TemplateNotFoundException for template.
    • Fix: Use the correct template path (e.g., App:Dummy:create.html.twig implies templates/App/Dummy/create.html.twig).
  4. Route Conflicts

    • Issue: Auto-generated routes clash with existing ones.
    • Fix: Explicitly set path in config or use _route in templates:
      {{ path('admin_dummy_create') }}
      
  5. Circular Dependencies

    • Issue: Actions depend on services that aren’t available during compilation.
    • Fix: Use lazy loading or ContainerAwareTrait:
      use Symfony\Component\DependencyInjection\ContainerAwareTrait;
      
      class ExportAction extends AbstractAction {
          use ContainerAwareTrait;
          public function execute() {
              $service = $this->container->get('some.service');
          }
      }
      

Debugging

  1. Dump Action Config Use a command to inspect configurations:

    php bin/console debug:container colin_action.action_handler
    

    Or dump in a controller:

    $handler = $this->get('colin_action.action_handler');
    dump($handler->getConfigurations());
    
  2. Enable Debug Mode Set APP_DEBUG=1 in .env to see detailed errors for missing templates, forms, or entities.

  3. Check Event Dispatcher Verify events are fired by adding a debug listener:

    $dispatcher->addListener('action.pre.execute', function (ActionEvent $event) {
        error_log('Action triggered: ' . $event->getActionName());
    });
    

Tips

  1. Reuse Configurations Extend default configurations for all actions:

    colin_action:
        configurations:
            create: App\Configuration\CustomCreateActionConfiguration
    

    Where CustomCreateActionConfiguration extends CreateActionConfiguration.

  2. Dynamic Templates Use Twig’s render function to dynamically include templates:

    {% render [controller('App\Controller\DummyController::templateAction', {'action': action})] %}
    
  3. Bulk Actions Group actions under a single namespace for better organization:

    actions:
        api:
            user:
                list: { type: list, configs: { entity_class: App\Entity\User } }
                create: { type: create, configs: { entity_class: App\Entity\User } }
    
  4. Localization Override templates per locale by creating locale-specific directories (e.g., templates/en/App/Dummy/create.html.twig).

  5. Performance Cache action configurations if they rarely change:

    $configs = $this->get('colin_action.action_handler')->getConfigurations();
    // Cache $configs for 1 hour
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware