alexander-a2/symfony-admin-bundle
Installation
composer require alexander-a2/symfony-admin-bundle
Register the bundle in config/bundles.php:
return [
// ...
AlexanderA2\SymfonyAdminBundle\SymfonyAdminBundle::class => ['all' => true],
];
Enable CRUD for an Entity
Create a YAML config file (e.g., config/packages/symfony_admin.yaml):
symfony_admin:
resources:
App\Entity\YourEntity:
title: "Your Entity"
list: [id, name, createdAt]
show: [id, name, description]
edit: [name, description]
First Use Case
Access /admin to see the auto-generated admin panel for YourEntity. No controller or template needed—just define the config.
Define Resources in Config Use YAML/XML/PHP to declare entities, fields, and actions:
symfony_admin:
resources:
App\Entity\User:
title: "Users"
list: [id, email, roles]
edit: [email, roles, isActive]
actions:
- { name: "ban", label: "Ban User" }
list: [posts.title] for User entity).Customize Field Types Override default form types via config:
App\Entity\User:
fields:
email:
type: "email"
roles:
type: "entity"
options:
class: App\Entity\Role
Extend with Controllers For advanced logic, create a custom controller:
// src/Controller/Admin/UserAdminController.php
namespace App\Controller\Admin;
use AlexanderA2\SymfonyAdminBundle\Controller\AbstractAdminController;
class UserAdminController extends AbstractAdminController
{
public function banAction($id): Response
{
$user = $this->getEntityManager()->getRepository(User::class)->find($id);
$user->setIsActive(false);
$this->getEntityManager()->flush();
return $this->redirectToRoute('admin_user_index');
}
}
Link it in config:
App\Entity\User:
controller: App\Controller\Admin\UserAdminController
EntityManager or extend the bundle.knp_menu:
symfony_admin:
menu:
- { route: 'admin_user_index', label: 'Users' }
{{ admin.generate_list_link(entity) }}
PHP 8.3+ Requirement Ensure your project uses PHP ≥8.3. Downgrade may break type hints or attributes.
Missing knp-menu-bundle
If the admin menu doesn’t appear, install:
composer require knplabs/knp-menu-bundle
And configure it in config/packages/knp_menu.yaml.
Field Overrides Not Applied Verify YAML indentation (2 spaces per level). Example:
# Correct
fields:
name:
type: "text"
options:
attr:
placeholder: "Enter name"
Actions Not Triggering
Ensure the action method exists in your custom controller and is listed in actions:
actions:
- { name: "ban", label: "Ban" } # Must match method name
Check Config Loading Dump the loaded config in a controller:
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
public function debugConfig(ParameterBagInterface $params): Response
{
return new Response(print_r($params->get('symfony_admin'), true));
}
Route: /debug/config.
Enable Debug Mode
Set APP_DEBUG=1 in .env to see detailed errors for missing entities or fields.
Custom Field Types
Create a service tagged as symfony_admin.field_type:
services:
App\FieldType\CustomType:
tags:
- { name: symfony_admin.field_type, alias: "custom" }
Implement AlexanderA2\SymfonyAdminBundle\Form\FieldTypeInterface.
Event Listeners
Subscribe to admin events (e.g., pre_save, post_delete):
use AlexanderA2\SymfonyAdminBundle\Event\AdminEvent;
public function onPreSave(AdminEvent $event): void
{
$entity = $event->getEntity();
$entity->setUpdatedAt(new \DateTime());
}
Tag the service:
services:
App\EventListener\AdminListener:
tags:
- { name: kernel.event_listener, event: symfony_admin.pre_save, method: onPreSave }
Override Templates
Copy templates from vendor/alexander-a2/symfony-admin-bundle/Resources/views/ to templates/admin/ to customize layouts or partials.
App\Entity\User:
bulk_actions:
- { name: "delete", label: "Delete Selected" }
isDeleted field and filter lists:
App\Entity\User:
list:
- { field: "id", label: "ID" }
- { field: "name", label: "Name" }
filters:
- { field: "isDeleted", operator: "!=", value: true }
paginator options:
App\Entity\User:
list:
paginator:
items_per_page: 50
How can I help you explore Laravel packages today?