简体   繁体   English

如何在SonataMediaBundle中覆盖MediaController并正确编写路由?

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

I've installed Sonata MediaBundle to manage media in my website. 我已经安装了Sonata MediaBundle来管理网站中的媒体。 Everything is working OK, but now I need to customize the viewAction of the MediaController. 一切正常,但是现在我需要自定义MediaController的viewAction。 I have copied the original MediaController and changed it. 我已经复制了原始MediaController并进行了更改。 It is seating in src/Application/Sonata/MediaBundle/Controller . 它位于src/Application/Sonata/MediaBundle/Controller I put it there because that folder was created when I extended the MediaBundle using easy-extends . 我将其放在此处是因为该文件夹是在使用easy-extends扩展MediaBundle时创建的。 I'm using annotations for the routing, so the relevant part of my routing file looks like this: 我正在使用注释进行路由,因此路由文件的相关部分如下所示:

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

My Controller looks this way: 我的控制器看起来像这样:

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

No changes so far except for the annotations. 到目前为止,除注释外没有任何更改。 When I try to access the URL http:// my-server /media/view/4 I get this error: 当我尝试访问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 

The bundle is loaded in the AppKernel.php: 该捆绑软件已加载到AppKernel.php中:

new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),

Any idea of what's the problem here? 知道这里有什么问题吗? I have tested various manners of writing the route, but nothing change. 我已经测试了编写路线的各种方式,但是没有任何变化。 Is there a better way to override the MediaCOntroller? 有没有更好的方法来覆盖MediaCOntroller? thanks in advance. 提前致谢。

Finally I got to know what had happened. 终于我知道发生了什么事。 Due to a refactor op, the _main route, that imported the routes form the routing.yml under app/config had changed and was trying to import a media.yml that didn't existed, of course. 由于重构操作的原因,从app/config下的routing.yml导入路由的_main路由已更改,并且正在尝试导入不存在的media.yml。 i discovered it thanks to the Local History feature that PHPStorm has. 我发现它的原因是PHPStorm具有“本地历史记录”功能。 Thanks to everyone that took a look to the question. 感谢所有看过这个问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM