【问题标题】:Sonata Admin Bundle custom routes using annotationsSonata Admin Bundle 使用注释的自定义路由
【发布时间】:2014-01-29 16:11:25
【问题描述】:

所以我在开发 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`.

这个消息明显是废话,这到底是怎么回事???

【问题讨论】:

    标签: symfony sonata-admin


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。与此同时,我注意到我需要配置“for symfony”路由才能使用“$this->get('router')->generate”和“for Sonata”创建 uri 才能访问页面。
    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 2015-04-22
    • 2012-07-15
    • 2016-09-03
    • 2017-08-05
    • 2016-04-20
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多