Reputation: 379
So I work on a Sonata Admin custom app. I'm trying to create a custom action to download a file in a controller that extends CRUDController.
The action is:
/**
* @Route("/download-list/{id}", name="download_list")
*/
public function downloadListAction($id = null) {
$id = $this->get('request')->get($this->admin->getIdParameter());
$object = $this->admin->getObject($id);
if (!$object) {
throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
}
if (false === $this->admin->isGranted('VIEW', $object)) {
throw new AccessDeniedException();
}
$entity = $this->admin->getSubject();
$exportFolder = $this->container->getParameter('export_folder');
$filePath = $this->get('kernel')->getRootDir() . $exportFolder . DIRECTORY_SEPARATOR . $entity->createListFileName() . $this->extension;
$response = new BinaryFileResponse($filePath);
$response->headers->set('Content-Type', 'text/plain');
return $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $entity->createListFileName());
}
In routing.yml
i have
spam_list_admin:
resource: "@SpamAdminCoreBundle/Controller/SpamListAdminController.php"
type: annotation
prefix: /list
The router debugger shows my route, i can generate the uri using $this->get('router')->generate('download_list', array('id' => $id))
but if I access it (app_dev.php/list/download-list/6) I get
There is no `_sonata_admin` defined for the controller `SpamAdmin\CoreBundle\Controller\SpamListAdminController` and the current route `download_list`.
This message is obviously crap, what is the real deal here???
Upvotes: 1
Views: 3250
Reputation: 456
Routes for Sonata CRUDController actions must be defined in your Admin class.
protected function configureRoutes(RouteCollection $collection)
{
parent::configureRoutes($collection);
$collection->add('download_list', $this->getRouterIdParameter().'/download-list')
}
Upvotes: 2