In the crud list context, you can export entities to a variety of formats.
Clicking the ‘Download’ button shows a list of available formats. Selecting one of these will download an export of these entities in the selected format.
Add routing_export.yml
from the BaseBundle to your routing configuration:
perform_base_export:
resource: "@PerformBaseBundle/Resources/config/routing_export.yml"
prefix: /admin/_export
This resource loads routes for a controller in the BaseBundle that handles the different actions.
Make sure to use a sensible prefix that won’t conflict with any existing routes, e.g. /admin/_export
.
Exporting options can be customized for each crud class with the Perform\BaseBundle\Crud\CrudInterface#configureExports()
method.
<?php
public function configureExports(ExportConfig $config)
{
}
The passed Perform\BaseBundle\Config\ExportConfig
instance can be used to customize how exporting works.
Use setFormats()
to change the available export formats.
A valid format is one of the ExportConfig::FORMAT_*
constants.
<?php
public function configureExports(ExportConfig $config)
{
$config->setFormats([
ExportConfig::FORMAT_JSON,
ExportConfig::FORMAT_CSV,
ExportConfig::FORMAT_XLS,
]);
}
Use the configureFormat()
method to configure a given format.
<?php
public function configureExports(ExportConfig $config)
{
$config->configureFormat(ExportConfig::FORMAT_CSV, [
'showHeaders' => false,
]);
}
Each format has different options:
delimiter
(string), enclosure
(string), escape
(string), showHeaders
(boolean), withBom
(boolean)showHeaders
(boolean)Use the setFilename()
method to set the name of the downloaded file.
It can be a string, or a function that takes the name of the format and returns a string.
<?php
public function configureExports(ExportConfig $config)
{
$config->setFilename('data'); // will return data.json, data.csv, etc
$config->setFilename(function($format) { return 'data_'.rand(1, 100).'.'.$format; });
}
By default, the filename will be a sensible suggestion for the current crud class (e.g. UserCrud
would become users.json
).
You can also show links to export entities in other places with the perform_export_dropdown
and perform_export_route
twig functions.
perform_export_dropdown
will render a dropdown with all available formats for that entity, and perform_export_route
will return a url for the given format.
Both require the entity alias or classname, e.g. AppBundle:Post
or AppBundle\Entity\Post
.
perform_export_route
also requires the format, and perform_export_dropdown
can optionally take a translation key for the dropdown label (the default is perform.export.dropdown
).
{{perform_export_dropdown('PerformUserBundle:User')}}
{{perform_export_dropdown('PerformUserBundle:User', 'app.download_label')}} <!-- custom translation label -->
<a href="{{perform_export_route('PerformUserBundle:User', 'json')}}">
Download users as json
</a>