ResponseGeneratorSubscriber.php

Same filename and directory in other branches
  1. 8.9.x core/lib/Drupal/Core/EventSubscriber/ResponseGeneratorSubscriber.php
  2. 10 core/lib/Drupal/Core/EventSubscriber/ResponseGeneratorSubscriber.php
  3. 11.x core/lib/Drupal/Core/EventSubscriber/ResponseGeneratorSubscriber.php

Namespace

Drupal\Core\EventSubscriber

File

core/lib/Drupal/Core/EventSubscriber/ResponseGeneratorSubscriber.php

View source
<?php

namespace Drupal\Core\EventSubscriber;

use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Response subscriber to add X-Generator header tag.
 */
class ResponseGeneratorSubscriber implements EventSubscriberInterface {
  
  /**
   * Sets extra X-Generator header on successful responses.
   *
   * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
   *   The event to process.
   */
  public function onRespond(ResponseEvent $event) {
    if (!$event->isMainRequest()) {
      return;
    }
    $response = $event->getResponse();
    // Set the generator in the HTTP header.
    [
      $version,
    ] = explode('.', \Drupal::VERSION, 2);
    $response->headers
      ->set('X-Generator', 'Drupal ' . $version . ' (https://www.drupal.org)');
  }
  
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
    ];
    return $events;
  }

}

Classes

Title Deprecated Summary
ResponseGeneratorSubscriber Response subscriber to add X-Generator header tag.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.