/** * Detect if we should use the FeedRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null|FeedRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof Model\FeedModel) { // FeedModel found return $this->renderer; } $request = $e->getRequest(); if (!$request instanceof HttpRequest) { // Not an HTTP request; cannot autodetermine return; } $headers = $request->getHeaders(); if (!$headers->has('accept')) { return; } $accept = $headers->get('accept'); if (($match = $accept->match('application/rss+xml, application/atom+xml')) == false) { return; } if ($match->getTypeString() == 'application/rss+xml') { $this->renderer->setFeedType('rss'); return $this->renderer; } if ($match->getTypeString() == 'application/atom+xml') { $this->renderer->setFeedType('atom'); return $this->renderer; } }
/** * Detect if we should use the FeedRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null|FeedRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof Model\FeedModel) { // FeedModel found return $this->renderer; } $request = $e->getRequest(); if (!$request instanceof HttpRequest) { // Not an HTTP request; cannot autodetermine return; } $headers = $request->headers(); if ($headers->has('accept')) { $accept = $headers->get('accept'); foreach ($accept->getPrioritized() as $mediaType) { if (0 === strpos($mediaType, 'application/rss+xml')) { // application/rss+xml Accept header found $this->renderer->setFeedType('rss'); return $this->renderer; } if (0 === strpos($mediaType, 'application/atom+xml')) { // application/atom+xml Accept header found $this->renderer->setFeedType('atom'); return $this->renderer; } } } // Not matched! return; }
/** * @param ViewEvent $e The ViewEvent instance * @return RendererInterface */ public function selectRenderer($e) { $request = $e->getRequest(); $headers = $request->getHeaders(); $model = $e->getModel(); // No Accept header? return PhpRenderer if (!$headers->has('accept')) { return $this->phpRenderer; } $accept = $headers->get('accept'); /* @var $mediaType \Zend\Http\Header\Accept\FieldValuePart\AcceptFieldValuePart */ foreach ($accept->getPrioritized() as $mediaType) { $mediaSubtype = $mediaType->getSubtype(); if ($mediaSubtype === 'json') { if (!$model instanceof JsonModel && ($children = $model->getChildrenByCaptureTo('content', false))) { $this->jsonRenderer->setMergeUnnamedChildren(true); foreach ($children as $child) { if (!$child instanceof JsonModel) { $child->setCaptureTo(null); } } } return $this->jsonRenderer; } if ($mediaSubtype === 'rss+xml' || $mediaSubtype === 'atom+xml') { $this->feedRenderer->setFeedType(substr($mediaSubtype, 0, strpos($mediaSubtype, '+'))); if (!$model instanceof FeedModel && ($children = $model->getChildrenByCaptureTo('content', false))) { foreach ($children as $child) { if (!$child instanceof FeedModel) { $child->setCaptureTo(null); } } } return $this->feedRenderer; } } // Nothing matched; return PhpRenderer. Technically, we should probably // return an HTTP 415 Unsupported response. return $this->phpRenderer; }