【问题标题】:How to override the MediaController in SonataMediaBundle and correctly write the routes?如何覆盖 SonataMediaBundle 中的 MediaController 并正确编写路由?
【发布时间】:2012-10-16 02:09:01
【问题描述】:

我已安装 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 

bundle 加载到 AppKernel.php 中:

new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),

知道这里有什么问题吗?我已经测试了编写路线的各种方式,但没有任何改变。有没有更好的方法来覆盖 MediaCONtroller? 提前致谢。

【问题讨论】:

    标签: symfony routes overriding bundle media


    【解决方案1】:

    我终于知道发生了什么事。由于重构操作,从 app/config 下的 routing.yml 导入路由的 _main 路由已更改,并试图导入一个不存在的 media.yml,当然。 由于 PHPStorm 具有的本地历史功能,我发现了它。 感谢所有关注这个问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 2013-02-27
      • 2023-03-26
      相关资源
      最近更新 更多