You are here

public function XmlSitemapController::renderSitemapXml in XML sitemap 8

Same name and namespace in other branches
  1. 2.x src/Controller/XmlSitemapController.php \Drupal\xmlsitemap\Controller\XmlSitemapController::renderSitemapXml()

Provides the sitemap in XML format.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object.

Return value

\Symfony\Component\HttpFoundation\BinaryFileResponse The sitemap in XML format or plain text if xmlsitemap_developer_mode flag is set.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException If the sitemap is not found or the sitemap file is not readable.

1 string reference to 'XmlSitemapController::renderSitemapXml'
xmlsitemap.routing.yml in ./xmlsitemap.routing.yml
xmlsitemap.routing.yml

File

src/Controller/XmlSitemapController.php, line 74

Class

XmlSitemapController
Class for Xml Sitemap Controller.

Namespace

Drupal\xmlsitemap\Controller

Code

public function renderSitemapXml(Request $request) {
  $headers = [];
  if ($this->state
    ->get('xmlsitemap_developer_mode')) {
    $headers['X-XmlSitemap-Current-Context'] = Json::encode(xmlsitemap_get_current_context());
    $headers['X-XmlSitemap'] = 'NOT FOUND';
  }
  $sitemap = XmlSitemap::loadByContext();
  if (!$sitemap) {
    $exception = new NotFoundHttpException();
    $exception
      ->setHeaders($headers);
    throw $exception;
  }
  $chunk = xmlsitemap_get_current_chunk($sitemap, $request);
  $file = xmlsitemap_sitemap_get_file($sitemap, $chunk);

  // Provide debugging information via headers.
  if ($this->state
    ->get('xmlsitemap_developer_mode')) {
    $headers['X-XmlSitemap'] = Json::encode($sitemap
      ->toArray());
    $headers['X-XmlSitemap-Cache-File'] = $file;
    $headers['X-XmlSitemap-Cache-Hit'] = file_exists($file) ? 'HIT' : 'MISS';
  }
  return $this
    ->getSitemapResponse($file, $request, $headers);
}