2lenet/cruditplatform-bundle
Installation:
composer require 2lenet/cruditplatform-bundle
Then enable the bundle in config/bundles.php:
return [
// ...
TwoLenet\CruditPlatformBundle\TwoLenetCruditPlatformBundle::class => ['all' => true],
];
Publish Configuration:
php bin/console cruditplatform:install
This generates default config files in config/packages/two_lenet_crudit_platform.yaml.
First Use Case:
Create a CRUD controller scaffold for an existing entity (e.g., Post):
php bin/console make:crudit Post
This generates a controller, routes, and basic CRUD logic in src/Controller/PostCruditController.php.
vendor/two_lenet/cruditplatform-bundle/README.md for CLI commands and conventions.make:crudit, inspect:
src/Controller/{Entity}CruditController.php (core logic).config/routes/crudit.yaml (auto-generated routes).config/packages/two_lenet_crudit_platform.yaml (e.g., form fields, permissions).Entity-Based CRUD:
Use make:crudit to scaffold controllers for entities. The bundle auto-detects Doctrine entities and generates:
index()).new(), create(), edit(), update(), delete()).FormBuilder).Customization:
Extend the base controller (src/Controller/BaseCruditController.php) to add business logic:
namespace App\Controller;
use TwoLenet\CruditPlatformBundle\Controller\BaseCruditController;
class PostCruditController extends BaseCruditController
{
protected function configureFormFields(): array
{
return [
'title' => 'text',
'content' => 'textarea',
// Override or add fields
];
}
}
Routing:
Routes are auto-generated in config/routes/crudit.yaml. Customize by extending the bundle’s router:
two_lenet_crudit_platform:
resource: '!@TwoLenetCruditPlatformBundle/Resources/config/routing.yaml'
prefix: '/admin' # Override base path
Permissions:
Use the access_control config to restrict actions:
two_lenet_crudit_platform:
access_control:
PostCruditController:
index: ROLE_ADMIN
create: ROLE_EDITOR
Doctrine Events:
Hook into lifecycle events (e.g., prePersist) in your extended controller:
protected function prePersist($entity): void
{
$entity->setCreatedAt(new \DateTime());
}
Form Extensions:
Extend the bundle’s form types (e.g., TwoLenet\CruditPlatformBundle\Form\Type\CruditFormType) to add custom fields or validation:
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use TwoLenet\CruditPlatformBundle\Form\Type\CruditFormType;
class ExtendedCruditFormType extends CruditFormType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('custom_field', TextType::class);
}
}
Then reference it in configureFormFields().
Twig Templates:
Override templates in templates/two_lenet_crudit_platform/ to customize UI (e.g., _form.html.twig).
API Support:
The bundle includes JSON responses by default. Extend BaseCruditController to add API-specific logic:
public function indexApi(Request $request): JsonResponse
{
$data = $this->indexAction($request);
return $this->json($data);
}
Entity Naming:
The make:crudit command expects Doctrine entities with setters/getters for fields. Avoid magic properties or __get()/__set() unless explicitly supported.
Configuration Overrides:
Changes to config/packages/two_lenet_crudit_platform.yaml require a cache clear:
php bin/console cache:clear
Form Field Mismatches: If a field in your entity isn’t reflected in the form, ensure:
configureFormFields().'text', 'date') matches the entity property type.Route Conflicts: The bundle auto-generates routes. Avoid naming conflicts by:
PostCruditController vs. posts).prefix in crudit.yaml.Debugging:
Enable debug mode in config/packages/dev/two_lenet_crudit_platform.yaml:
debug: true
This logs SQL queries and form data.
Check Generated SQL: Use Doctrine’s query logging:
php bin/console debug:config doctrine
Set SQL logging to true.
Form Debugging: Dump form data in your controller:
public function create(Request $request)
{
$form = $this->createForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
dump($form->getData()); // Debug submitted data
}
}
Event Listeners: Add a listener to debug entity events:
namespace App\EventListener;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
class CruditDebugSubscriber implements EventSubscriber
{
public function getSubscribedEvents(): array
{
return ['prePersist', 'preUpdate'];
}
public function prePersist(LifecycleEventArgs $args): void
{
dump($args->getObject());
}
}
Register it in services.yaml:
services:
App\EventListener\CruditDebugSubscriber:
tags: ['doctrine.event_subscriber']
Custom Actions:
Add actions to the controller by extending BaseCruditController:
public function publish(Request $request, $id)
{
$entity = $this->getEntityManager()->find($this->entityClass, $id);
$entity->setStatus('published');
$this->getEntityManager()->flush();
return $this->redirectToIndex();
}
Add the route in config/routes/crudit.yaml:
publish:
path: /{id}/publish
methods: [POST]
defaults: { _controller: 'App\Controller\PostCruditController::publish' }
Dynamic Field Mapping:
Override configureFormFields() to dynamically map fields based on entity metadata:
protected function configureFormFields(): array
{
$fields = [];
foreach ($this->getEntityManager()->getClassMetadata($this->entityClass)->getFieldNames() as $field) {
$fields[$field] = match ($field) {
'published_at' => 'datetime',
default => 'text',
};
}
return $fields;
}
Bulk Actions: Extend the index action to support bulk operations:
public function index(Request $request)
{
$entities = $this->getEntities();
$form = $this->createBulkForm($entities)->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->bulkAction($form->getData());
}
return $this->renderIndex($entities);
}
Soft Deletes:
Integrate with gedmo/doctrine-extensions for soft deletes:
two_lenet_crudit_platform:
soft_delete: true
Then extend the delete() action to use soft delete logic.
Default Values:
The bundle assumes id as the primary key. For UUIDs or custom IDs, override the idField in config:
two_lenet_crudit_platform:
entities:
Post:
id_field: uuid
Translation: Form labels/placeholders use Symfony’s translation system. Ensure your translations are loaded:
# config/packages/translation.yaml
How can I help you explore Laravel packages today?