1

I am trying to get my head around using EventSubscriber for adding HTTP Headers. In D7 I would have simply used

drupal_add_http_header('Content-Type', 'application/javascript');

But I see that this is now deprecated in D8. I have been hunting on Google for hours and it seems I need to use EventSubscriber now. My understanding of the EventSubscriber in D8 is a bit weak

I have created an EventSubscriber directory in my module

In that directory I added a file called Unsw_blocksSubscriber.php and it contains the following code

<?php

namespace Drupal\unsw_blocks\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class HeaderResponseSubscriber implements EventSubscriberInterface {
  public function onRespond(FilterResponseEvent $event) {
    $response = $event->getResponse();
    $response->headers->set('Content-Type', 'application/javascript');
  }
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = array('onRespond');
    return $events;
  }
}

I also added unsw_blocks.services.yml in my module and it contains the following code

services:
  unsw_blocks.subscriber:
    class: Drupal\unsw_blocks\EventSubscriber\Unsw_blocksSubscriber
    tags:
      - { name: event_subscriber }

When I try to view my site I am getting he following error

ReflectionException: Class Drupal\\unsw_blocks\\EventSubscriber\\Unsw_blocksSubscriber does not exist in C:\\xampp\\htdocs\\unsw-d8-modules\\core\\lib\\Drupal\\Core\\DependencyInjection\\Compiler\\RegisterEventSubscribersPass.php on line 30 #0 C:\\xampp\\htdocs\\unsw-d8-modules\\core\\lib\\Drupal\\Core\\DependencyInjection\\Compiler\\RegisterEventSubscribersPass.php(30): ReflectionClass->__construct('Drupal\\\\unsw_blo...')\n#1 C:\\xampp\\htdocs\\unsw-d8-modules\\vendor\\symfony\\dependency-injection\\Compiler\\Compiler.php(120): Drupal\\Core\\DependencyInjection\\Compiler\\RegisterEventSubscribersPass->process(Object(Drupal\\Core\\DependencyInjection\\ContainerBuilder))\n#2 C:\\xampp\\htdocs\\unsw-d8-modules\\vendor\\symfony\\dependency-injection\\ContainerBuilder.php(573): Symfony\\Component\\DependencyInjection\\Compiler\\Compiler->compile(Object(Drupal\\Core\\DependencyInjection\\ContainerBuilder))\n#3 C:\\xampp\\htdocs\\unsw-d8-modules\\core\\lib\\Drupal\\Core\\DrupalKernel.php(1307): Symfony\\Component\\DependencyInjection\\ContainerBuilder->compile()\n#4 C:\\xampp\\htdocs\\unsw-d8-modules\\core\\lib\\Drupal\\Core\\DrupalKernel.php(884): Drupal\\Core\\DrupalKernel->compileContainer()\n#5 C:\\xampp\\htdocs\\unsw-d8-modules\\core\\lib\\Drupal\\Core\\DrupalKernel.php(466): Drupal\\Core\\DrupalKernel->initializeContainer()\n#6 C:\\xampp\\htdocs\\unsw-d8-modules\\core\\lib\\Drupal\\Core\\DrupalKernel.php(656): Drupal\\Core\\DrupalKernel->boot()\n#7 C:\\xampp\\htdocs\\unsw-d8-modules\\index.php(19): Drupal\\Core\\DrupalKernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request))\n#8 {main}

It appears I am making what is probably a simple mistake but I am having trouble finding my issue. I would appreciate it if someone could steer me in the right direction

5
  • 1
    The class could not be found because the php filename has to be the same as the class name: src/EventSubscriber/HeaderResponseSubscriber.php and use the same class name in the service file. – 4k4 Dec 14 '17 at 7:29
  • I have made the changes as suggested and I am now getting ReflectionException: Class Drupal\\unsw_blocks\\EventSubscriber\\HeaderResponseSubscriber does not exist in C:\\xampp\\htdocs\\unsw-d8-modules\\core\\lib\\Drupal\\Core\\DependencyInjection\\Compiler\\RegisterEventSubscribersPass.php on line 30 – rickl Dec 14 '17 at 19:49
  • see this example drupal.stackexchange.com/questions/188924/… – 4k4 Dec 14 '17 at 19:58
  • I looked at that example and it seems to mirror my code and I can't see anything in there that would help me – rickl Dec 15 '17 at 3:39
  • the Class name and the php file name are now correct. I am still hunting for a solution. Does anyone have any further ideas to try? – rickl Dec 17 '17 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.