Installation:
composer require elao/admin-bundle
Register the bundle in AppKernel.php and import routing in routing.yml as shown in the README.
Define a Model:
Ensure your model (e.g., User) is a Doctrine entity with proper annotations/repositories.
Configure an Admin:
Create a YAML configuration file (e.g., app/config/admin.yml) to define actions for your model:
elao_admin:
models:
App\Entity\User:
actions:
list: ~
create: ~
edit: { route_parameters: { id: id } }
delete: ~
First Use Case:
Access /users (or your prefixed route) to see a generated list of users. The bundle auto-registers routes like /users, /users/create, /users/{id}/edit, etc.
Action Definition:
Define actions (e.g., list, create, edit, delete) in admin.yml for each model. Use built-in actions or extend them:
actions:
custom_action:
class: App\Admin\Action\CustomAction
arguments: [@service_id]
Template Overrides:
Override default templates by placing them in templates/ElaoAdminBundle/[ActionName]/[template].html.twig. Example:
templates/ElaoAdminBundle/List/list.html.twig
Dynamic Routing:
Leverage the bundle’s auto-routing. For a model App\Entity\Post, routes like /posts/{id}/publish are auto-generated if publish is defined in actions.
Service Integration:
Inject services into actions via arguments in YAML or constructor binding:
actions:
export:
class: App\Admin\Action\ExportAction
arguments: [@doctrine.orm.entity_manager, @some_service]
Batch Actions:
Use the batch action for bulk operations (e.g., delete, export):
actions:
batch_delete: ~
id fields and repositories.isGranted() in custom actions:
public function isGranted($attribute, $object = null)
{
return $this->security->isGranted('ROLE_ADMIN');
}
actions:
create:
listeners:
- App\EventListener\UserCreateListener
Elao\Bundle\AdminBundle\Form\Type\AdminType for custom forms:
class UserType extends AdminType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
}
}
Configure in YAML:
actions:
edit:
form_type: App\Form\Type\UserType
Outdated Package: The bundle is archived (last release: 2017). Test thoroughly in your environment, especially with newer Symfony/Laravel versions (though this is Symfony-specific). Consider forking if critical bugs arise.
Routing Conflicts:
Prefix routes in routing.yml to avoid clashes with other bundles:
prefix: /admin
Otherwise, /users may conflict with FOSUserBundle or other routes.
Action Configuration Overrides: YAML configuration is merged, so later definitions override earlier ones. Order matters:
# app/config/admin.yml
models:
App\Entity\User:
actions:
list: { template: "custom_list.html.twig" }
Template Inheritance:
Overriding templates requires exact directory structure. Misplaced templates (e.g., templates/ElaoAdminBundle/List/list.html.twig vs. templates/bundles/elaoadmin/list.html.twig) will fail silently.
Doctrine Proxy Issues: If using proxies, ensure your actions handle lazy-loading properly. Example:
public function preExecute()
{
$this->getObject()->load(); // Force load if needed
}
Route Debugging:
Use Symfony’s debug:router command to verify auto-generated routes:
php bin/console debug:router | grep elao_admin
Action Debugging:
Enable debug mode (APP_DEBUG=true) to see action instantiation errors. Check var/log/dev.log for YAML parsing issues.
Event Listener Debugging: Add logging in listeners to trace execution:
public function onPreAction()
{
$this->logger->debug('Action executed for object:', ['id' => $this->getObject()->getId()]);
}
Custom Actions:
Extend Elao\Bundle\AdminBundle\Action\AbstractAction for reusable logic:
class ExportAction extends AbstractAction
{
protected function execute()
{
// Custom logic
}
}
Dynamic Action Configuration: Use PHP callbacks in YAML for dynamic action setup:
actions:
custom:
class: App\Admin\Action\DynamicAction
arguments: ["%kernel.root_dir%/config/dynamic_actions.php"]
Model-Specific Logic:
Override getModelClass() in custom actions to support polymorphic behavior:
public function getModelClass()
{
return $this->getRequest()->attributes->get('model_class');
}
Asset Management: Override CSS/JS by extending the bundle’s assets:
{% block elao_admin_stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="{{ asset('bundles/elaoadmin/custom.css') }}">
{% endblock %}
delete: ~) disables it entirely. Explicitly define all actions you need.route_parameters to customize route paths:
actions:
edit: { route_parameters: { _route: 'app_user_edit' } }
ElaoAdminBundle as the default translation domain. Override in YAML:
actions:
list: { translation_domain: 'messages' }
How can I help you explore Laravel packages today?