2014-01-10 34 views
1

因此,我致力於Sonata Admin自定義應用程序。我試圖創建一個自定義操作來下載擴展CRUDController的控制器中的文件。Sonata Admin使用註釋捆綁自定義路線

的操作是:

/** 
    * @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

Sonata CRUDController操作的路由必須在Admin類中定義。

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

謝謝。在此期間,我注意到我需要配置路徑「for symfony」,以便能夠使用「$ this-> get('router') - > generate」創建uri並且還能夠訪問「Sonata」這一頁。 –