Installation
composer require maximecolin/colin-action-bundle
Add to config/bundles.php:
return [
// ...
MaximeColin\Bundle\ActionBundle\ColinActionBundle::class => ['all' => true],
];
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
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).
Define Configuration
Extend default configurations in config/packages/colin_action.yaml for custom behavior (e.g., add validation rules, change templates).
Entity & Form Setup
Ensure your entity (App\Entity\Dummy) and form (App\Form\DummyType) exist and are referenced in the config.
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.
Routing
Routes are auto-generated under the admin namespace. Override paths in config if needed:
configs:
path: /custom/dummy/create
Accessing Actions Use the router or generate URLs programmatically:
$url = $this->generateUrl('admin_dummy_create');
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');
}
}
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
Configure Dependencies
Inject services into your action class via constructor (use ContainerAwareTrait if needed).
# 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
$eventDispatcher->addListener('action.pre.execute', function (ActionEvent $event) {
if ($event->getActionName() === 'admin.dummy.create') {
// Pre-execution logic
}
});
$action = $this->createMock(\App\Action\ExportAction::class);
$this->container->set('app.action.export', $action);
Entity Class Not Found
entity_class in config points to a non-existent class.App\Entity\Dummy) and namespace autoloading.Form Type Mismatch
form_type references a form that doesn’t exist or isn’t registered as a type.// src/Form/DummyType.php
class DummyType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
// ...
}
}
And is autowired or manually registered.Template Not Found
TemplateNotFoundException for template.App:Dummy:create.html.twig implies templates/App/Dummy/create.html.twig).Route Conflicts
path in config or use _route in templates:
{{ path('admin_dummy_create') }}
Circular Dependencies
ContainerAwareTrait:
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class ExportAction extends AbstractAction {
use ContainerAwareTrait;
public function execute() {
$service = $this->container->get('some.service');
}
}
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());
Enable Debug Mode
Set APP_DEBUG=1 in .env to see detailed errors for missing templates, forms, or entities.
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());
});
Reuse Configurations Extend default configurations for all actions:
colin_action:
configurations:
create: App\Configuration\CustomCreateActionConfiguration
Where CustomCreateActionConfiguration extends CreateActionConfiguration.
Dynamic Templates
Use Twig’s render function to dynamically include templates:
{% render [controller('App\Controller\DummyController::templateAction', {'action': action})] %}
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 } }
Localization
Override templates per locale by creating locale-specific directories (e.g., templates/en/App/Dummy/create.html.twig).
Performance Cache action configurations if they rarely change:
$configs = $this->get('colin_action.action_handler')->getConfigurations();
// Cache $configs for 1 hour
How can I help you explore Laravel packages today?