簡體   English   中英

如何在SonataMediaBundle中覆蓋MediaController並正確編寫路由?

[英]How to override the MediaController in SonataMediaBundle and correctly write the routes?

我已經安裝了Sonata MediaBundle來管理網站中的媒體。 一切正常,但是現在我需要自定義MediaController的viewAction。 我已經復制了原始MediaController並進行了更改。 它位於src/Application/Sonata/MediaBundle/Controller 我將其放在此處是因為該文件夾是在使用easy-extends擴展MediaBundle時創建的。 我正在使用注釋進行路由,因此路由文件的相關部分如下所示:

media:
    resource: '@Application/Sonata/MediaBundle/Controller'
    type:     annotation
    prefix: /media

我的控制器看起來像這樣:

 /**
 * Log controller.
 *
 * @Route("/media")
 */
class MediaController extends BaseMediaController {
/**
 * @throws NotFoundHttpException
 *
 * @param string $id
 * @param string $format
 *
 * @return Response
 *
 * @Route("/view/{video_id}", name="media_view")
 * @Method("GET")
 * @Template()
 */
public function viewAction($id, $format = 'reference')
{
    $media = $this->getMedia($id);

    if (!$media) {
        throw new NotFoundHttpException(sprintf('unable to find the media with the id : %s', $id));
    }

    if (!$this->get('sonata.media.pool')->getDownloadSecurity($media)->isGranted($media, $this->getRequest())) {
        throw new AccessDeniedException();
    }

    return $this->render('SonataMediaBundle:Media:view.html.twig', array(
        'media'     => $media,
        'formats'   => $this->get('sonata.media.pool')->getFormatNamesByContext($media->getContext()),
        'format'    => $format
    ));
}
}

到目前為止,除注釋外沒有任何更改。 當我嘗試訪問URL http:// my-server /media/view/4此錯誤:

 Cannot load resource "@Application/Sonata/MediaBundle/Controller". Make sure the "Application/Sonata/MediaBundle/Controller" bundle is correctly registered and loaded in the application kernel class.
500 Internal Server Error - FileLoaderLoadException 

該捆綁軟件已加載到AppKernel.php中:

new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),

知道這里有什么問題嗎? 我已經測試了編寫路線的各種方式,但是沒有任何變化。 有沒有更好的方法來覆蓋MediaCOntroller? 提前致謝。

終於我知道發生了什么事。 由於重構操作的原因,從app/config下的routing.yml導入路由的_main路由已更改,並且正在嘗試導入不存在的media.yml。 我發現它的原因是PHPStorm具有“本地歷史記錄”功能。 感謝所有看過這個問題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM