Installation Add the bundle via Composer:
composer require ecommit/crud-bundle
Enable the bundle in config/bundles.php:
Ecommit\CrudBundle\EcommitCrudBundle::class => ['all' => true],
First Use Case: Basic CRUD Controller
Generate a scaffolded controller for an Article entity:
php bin/console make:crud Article
This creates a controller with basic CRUD actions (index, create, edit, show, delete) in src/Controller/ArticleController.php.
Where to Look First
config/packages/ecommit_crud.yaml for default settings.Customizing CRUD Actions Override methods in the generated controller to add logic:
// src/Controller/ArticleController.php
public function edit(Request $request, Article $article, EntityManagerInterface $em)
{
// Custom logic before edit
$form = $this->createForm(ArticleType::class, $article);
// ...
}
Adding Fields to Forms
Extend the auto-generated ArticleType form class:
// src/Form/ArticleType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('custom_field', TextType::class);
}
Integration with Existing Projects
templates/EcommitCrudBundle/ to customize UI.use statements).API-First Approach Use the bundle’s built-in serialization (JMS Serializer or Symfony Serializer) for API responses:
# config/packages/ecommit_crud.yaml
ecommit_crud:
serialization: true
Bulk Actions Add bulk delete/edit functionality by extending the controller:
public function bulkDelete(Request $request)
{
$ids = $request->request->get('ids');
// Logic to delete multiple records
}
French Documentation
Auto-Generated Code Overrides
ArticleController.php in src/Controller/ and override methods).// src/Controller/ArticleController.php (extends auto-generated)
class ArticleController extends AbstractCrudController
{
public function configureFields(string $action): iterable
{
return parent::configureFields($action) + [
'custom_field' => 'text',
];
}
}
Entity Requirements
id field (auto-incremented primary key).SoftDeletable trait or a deletedAt column.Routing Conflicts
config/routes.yaml:
ecommit_crud_article:
resource: '@EcommitCrudBundle/Resources/config/routing/article.yaml'
prefix: '/admin'
JavaScript Dependencies
webpack.config.js includes:
Encore
.enableStimulus()
.addEntry('app', './assets/app.js');
Check the debug:config Command
Verify bundle settings:
php bin/console debug:config ecommit_crud
Enable Profiler Use Symfony’s profiler to inspect form submissions or entity changes:
// config/packages/dev/debug.yaml
framework:
profiler: { only_exceptions: false }
Log Form Errors Add this to your controller to debug form validation:
public function create(Request $request)
{
$form = $this->createForm(ArticleType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && !$form->isValid()) {
\Log::error('Form errors:', $form->getErrors(true));
}
}
Custom Actions Add new actions by extending the controller:
public function publish(Article $article)
{
$article->setIsPublished(true);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('article_index');
}
Event Listeners
Subscribe to bundle events (e.g., crud.pre_save) in config/services.yaml:
services:
App\EventListener\CrudListener:
tags:
- { name: kernel.event_listener, event: crud.pre_save, method: onPreSave }
Template Overrides
Copy the default template from vendor/ecommit/crud-bundle/templates/ to templates/EcommitCrudBundle/ and modify.
Translation
Extend translations in translations/messages.fr.yaml or messages.en.yaml:
# messages.en.yaml
crud:
article:
actions:
publish: "Publish Article"
Testing Use the bundle’s test utilities in PHPUnit:
public function testCrudActions()
{
$client = static::createClient();
$crawler = $client->request('GET', '/article');
$this->assertSelectorTextContains('h1', 'Articles');
}
```markdown
---
How can I help you explore Laravel packages today?