The AropixelAdminBundle provides a custom command to quickly generate a CRUD (Create, Read, Update, Delete) interface based on an existing FormType.
In line with the bundle's philosophy, this command does not aim to provide a "one-size-fits-all" solution. Instead, it generates the boilerplate code (Controller and Templates) that you can then fully customize to fit your specific needs.
To generate a new CRUD, run the following command:
php bin/console aropixel:make:crudThe command will ask you for:
- Entity Class: The full namespace of your entity (e.g.,
App\Entity\MyEntity). - FormType Class: The full namespace of your FormType (e.g.,
App\Form\MyEntityType).
The command generates three main files:
Created in src/Controller/Admin/. It includes:
- index: A route using the
DataTableFactoryto list your entities. - new: A route to create a new entity using your
FormType. - edit: A route to edit an existing entity.
- delete: A route to delete an entity with CSRF protection.
Created in templates/admin/[entity]/index.html.twig. It extends @AropixelAdmin/List/datatable.html.twig and is pre-configured to work with the Controller's DataTable.
Created in templates/admin/[entity]/form.html.twig. It extends @AropixelAdmin/Form/base.html.twig and provides a consistent look and feel for both "new" and "edit" actions.
By default, only the ID column is generated in the index action. You should manually add the columns you want to display in your Controller:
// src/Controller/Admin/MyEntityController.php
public function index(DataTableFactory $dataTableFactory, DataTableRowFactoryInterface $rowFactory): Response
{
return $dataTableFactory
->create(MyEntity::class)
->setColumns([
['label' => 'ID', 'orderBy' => 'id'],
['label' => 'Title', 'orderBy' => 'title'],
['label' => 'Created At', 'orderBy' => 'createdAt'],
['label' => '', 'orderBy' => '', 'class' => 'no-sort'],
])
->render($rowFactory);
}And update the template accordingly:
{# templates/admin/my_entity/index.html.twig #}
{% block datatable_body %}
<td>{{ item.id }}</td>
<td>{{ item.title }}</td>
<td>{{ item.createdAt|date('d/m/Y') }}</td>
<td class="text-right">
{{ list.actions(item, path('admin_myentity_edit', {id: item.id}), path('admin_myentity_delete', {id: item.id})) }}
</td>
{% endblock %}The generated form template uses {{ form_rest(form) }}. You can customize the layout by overriding the formbody or mainPanel blocks as described in the Form Templates documentation.