Sonata Admin使用注释捆绑自定义路由

时间:2014-01-10 15:47:22

标签: symfony sonata-admin

所以我在Sonata Admin自定义应用上工作。我正在尝试创建一个自定义操作来下载扩展CRUDController的控制器中的文件。

行动是:

    /**
     * @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());
    }

routing.yml我有

spam_list_admin:
    resource: "@SpamAdminCoreBundle/Controller/SpamListAdminController.php"
    type: annotation
    prefix: /list

路由器调试器显示我的路由,我可以使用$this->get('router')->generate('download_list', array('id' => $id))生成uri但是如果我访问它(app_dev.php / list / download-list / 6)我得到

There is no `_sonata_admin` defined for the controller `SpamAdmin\CoreBundle\Controller\SpamListAdminController` and the current route `download_list`.

这条消息显然是垃圾,这里有什么真正的交易???

1 个答案:

答案 0 :(得分:2)

必须在Admin类中定义Sonata CRUDController操作的路由。

protected function configureRoutes(RouteCollection $collection)
{
    parent::configureRoutes($collection);
    $collection->add('download_list', $this->getRouterIdParameter().'/download-list')
}