I am trying to create a basic event subscriber, but when I add the services.yml file and attempt to rebuild the cache with drush cr
, I get this error.
In Drupal.php line 130:
\Drupal::$container
is not initialized yet.\Drupal::setContainer()
must be called with a real container.
If I clear the cache from Drupal, it works fine, but Drush always errors out. If I remove the services file, Drush works fine again.
Has anyone seen this before? I don't want to move forward without resolving this. I have added services and event subscribers in this project before without an issue. I double checked spacing structure, etc.
module/gary_custom.services.yml
services:
gary_custom.custom_event_subscriber:
class: Drupal\gary_custom\EventSubscriber\CustomEventSubscriber
tags:
- { name: event_subscriber }
module/src/EventSubscriber/CustomEventSubscriber.php
namespace Drupal\gary_custom\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
class CustomEventSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
\Drupal::logger('debug')->debug('get sub events');
$events[KernelEvents::REQUEST][] = array('checkForRedirection');
return $events;
}
/**
* This method is called whenever the KernelEvents::REQUEST event is
* dispatched.
*
* @param GetResponseEvent $event
*/
public function checkForRedirection(GetResponseEvent $event) {
\Drupal::logger('debug')->debug('hitting');
}
}
I am using Drush 9.4.0.
getSubscribedEvents
method in particular?