SimplesitemapController.php 1.07 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1 2 3
<?php
/**
 * @file
gbyte.co's avatar
gbyte.co committed
4
 * Contains \Drupal\simple_sitemap\Controller\SimplesitemapController.
gbyte.co's avatar
gbyte.co committed
5 6
 */

gbyte.co's avatar
gbyte.co committed
7
namespace Drupal\simple_sitemap\Controller;
gbyte.co's avatar
gbyte.co committed
8

9
use Drupal\Core\Cache\CacheableResponse;
gbyte.co's avatar
gbyte.co committed
10 11 12 13 14 15 16 17
use Symfony\Component\HttpFoundation\Response;

/**
 * SimplesitemapController.
 */
class SimplesitemapController {

  /**
18
   * Returns the whole sitemap, a requested sitemap chunk, or the sitemap index file.
19 20
   *
   * @param int $sitemap_id
21 22
   *  Optional ID of the sitemap chunk. If none provided, the first chunk or
   *  the sitemap index is fetched.
23 24 25
   *
   * @return object Response
   *  Returns an XML response.
gbyte.co's avatar
gbyte.co committed
26
   */
27
  public function getSitemap($sitemap_id = NULL) {
28
    $sitemap = \Drupal::service('simple_sitemap.generator');
29
    $output = $sitemap->getSitemap($sitemap_id);
30
    $output = !$output ? '' : $output;
gbyte.co's avatar
gbyte.co committed
31

32
    // Display sitemap with correct xml header.
33 34 35 36
    $response = new CacheableResponse($output, Response::HTTP_OK, array('content-type' => 'application/xml'));
    $meta_data = $response->getCacheableMetadata();
    $meta_data->addCacheTags(['simple_sitemap']);
    return $response;
gbyte.co's avatar
gbyte.co committed
37 38
  }
}