eduardoledo/generic-admin-bundle
Installation:
composer require eduardoledo/generic-admin-bundle
php composer.phar update eduardoledo/generic-admin-bundle
Ensure FOSUserBundle, PagerBundle, and TinymceBundle are installed (automatically handled by the package).
Enable the Bundle:
Add to AppKernel.php:
new Lomaswifi\AdminBundle\LomaswifiAdminBundle(),
First Use Case:
Create a basic CRUD controller for an entity (e.g., Post):
php app/console lomaswifi:admin:generate --entity=Post --bundle=AppBundle
This generates a controller, routes, and admin configuration.
Configuration:
Edit app/config/config.yml to include:
lomaswifi_admin:
bundles:
- AppBundle
default_per_page: 20
Routing:
Import routes in app/config/routing.yml:
lomaswifi_admin:
resource: "@LomaswifiAdminBundle/Resources/config/routing.yml"
prefix: /admin
Entity-Based CRUD:
php app/console lomaswifi:admin:generate --entity=User --bundle=AppBundle
app/config/admin.yml):
lomaswifi_admin:
entities:
AppBundle\Entity\User:
fields:
- { name: username, type: text }
- { name: email, type: email }
Field Types:
Leverage built-in types (text, email, textarea, tinymce, date, etc.) or extend with custom types:
fields:
- { name: content, type: tinymce }
- { name: publishedAt, type: date }
Permissions:
Restrict access via FOSUserBundle roles (e.g., ROLE_ADMIN):
entities:
AppBundle\Entity\Post:
access_control: ROLE_ADMIN
Pagination: Configure per-page limits globally or per-entity:
lomaswifi_admin:
default_per_page: 10
Reusable Admin Panels: Share configurations across entities using inheritance:
entities:
AppBundle\Entity\BaseEntity:
fields:
- { name: createdAt, type: date, readonly: true }
AppBundle\Entity\Post:
extends: AppBundle\Entity\BaseEntity
Doctrine Events:
Hook into prePersist/preUpdate for validation or logic:
// In your entity
public function prePersist()
{
$this->setSlug(Str::slug($this->title));
}
Custom Actions:
Add buttons/actions (e.g., "Publish") in admin.yml:
entities:
AppBundle\Entity\Post:
actions:
- { name: publish, route: app_post_publish, icon: fa-check }
Form Extensions:
Extend forms via event listeners (e.g., add a published checkbox):
// src/AppBundle/EventListener/AdminFormListener.php
public function onBuildForm(FormEvent $event)
{
$form = $event->getForm();
$form->add('published', CheckboxType::class);
}
API Integration: Use the generated CRUD as a backend for frontend frameworks (e.g., React/Vue) by exposing routes:
# app/config/routing.yml
app_admin_api:
resource: "@LomaswifiAdminBundle/Resources/config/routing.yml"
prefix: /api/admin
defaults: { _format: json }
Asset Management:
Override templates (e.g., edit.html.twig) in AppBundle/Resources/LomaswifiAdminBundle/views/.
Bundle Naming:
The bundle uses LomaswifiAdminBundle in code but is named GenericAdminBundle in the package. Ensure consistency in AppKernel.php and commands.
FOSUserBundle Dependency:
composer require friendsofsymfony/user-bundle makerlabs/pager-bundle stfalcon/tinymce-bundle
TinymceBundle Conflicts:
TinymceBundle but may clash with existing configs. Override in config.yml:
stfalcon_tinymce:
selector: "textarea.tinymce"
plugins: ["advlist autolink lists link charmap print preview anchor"]
Entity Generation Issues:
generate command may fail if the entity lacks a id field or has complex relationships. Manually define fields in admin.yml if needed.Routing Conflicts:
lomaswifi_admin:
prefix: /admin
Command Errors:
src/AppBundle/Controller/Admin/ and app/config/admin.yml for typos.-v for verbose output:
php app/console lomaswifi:admin:generate -v --entity=Post
Template Overrides:
php app/console cache:clear
Permission Denied:
ROLE_ADMIN). Test with:
php app/console fos:user:create admin admin@example.com --super-admin
Field Validation:
admin.yml match the entity’s properties. Use doctrine:schema:validate to check:
php app/console doctrine:schema:validate
Custom Field Types:
Create a service for new field types (e.g., select2):
# services.yml
services:
app.admin.field.select2:
class: AppBundle\Form\Type\Select2Type
tags:
- { name: lomaswifi_admin.field_type, alias: select2 }
Event Listeners:
Subscribe to admin events (e.g., AdminEntityEvent):
// src/AppBundle/EventListener/AdminListener.php
public function onPreSave(AdminEntityEvent $event)
{
$entity = $event->getEntity();
$entity->setUpdatedAt(new \DateTime());
}
Register in services.yml:
services:
app.admin.listener:
class: AppBundle\EventListener\AdminListener
tags:
- { name: kernel.event_listener, event: lomaswifi.admin.pre_save, method: onPreSave }
Dynamic Field Mapping:
Use callbacks in admin.yml for dynamic values:
fields:
- { name: status, type: text, callback: getStatusLabel }
Implement the callback in your entity or service.
Bulk Actions: Extend bulk operations via custom routes and controllers:
# routing.yml
app_admin_bulk:
path: /admin/{entity}/bulk
defaults: { _controller: AppBundle:Admin\BulkController::index }
Internationalization: Translate field labels/placeholders using Symfony’s translation system:
# app/config/admin.yml
entities:
AppBundle\Entity\Post:
fields:
- { name: title, label: "admin.post.title" }
Add translations in translations/messages.en.yml:
admin:
post:
title: "Post Title"
How can I help you explore Laravel packages today?