简体   繁体   中英

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

I've installed Sonata MediaBundle to manage media in my website. Everything is working OK, but now I need to customize the viewAction of the MediaController. I have copied the original MediaController and changed it. It is seating in src/Application/Sonata/MediaBundle/Controller . I put it there because that folder was created when I extended the MediaBundle using easy-extends . 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:

 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:

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? 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. i discovered it thanks to the Local History feature that PHPStorm has. Thanks to everyone that took a look to the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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