コード例 #1
0
 /**
  * Determines and sets the Request format
  *
  * @param GetResponseEvent $event The event
  *
  * @throws NotAcceptableHttpException
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $format = null;
     if ($this->formatNegotiator instanceof MediaTypeNegotiatorInterface) {
         $mediaType = $this->formatNegotiator->getBestMediaType($request);
         if ($mediaType) {
             $request->attributes->set('media_type', $mediaType);
             $format = $request->getFormat($mediaType);
         }
     } else {
         $format = $this->formatNegotiator->getBestFormat($request);
     }
     if (null === $format) {
         if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
             throw new NotAcceptableHttpException("No matching accepted Response format could be determined");
         }
         return;
     }
     $request->setRequestFormat($format);
 }