Buy

Event Arguments and the Request

We now know that when you listen to this event, it passes you an event object called GetResponseEvent. Type-hint the argument to enjoy auto-completion:

28 lines modules/dino_roar/src/Jurassic/DinoListener.php
... lines 1 - 5
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
... lines 7 - 8
class DinoListener implements EventSubscriberInterface
{
public function onKernelRequest(GetResponseEvent $event)
{
... lines 13 - 18
}
... lines 20 - 27
}

Using the Event Argument

This event object has a getRequest() method, use that to set a new $requestvariable:

28 lines modules/dino_roar/src/Jurassic/DinoListener.php
... lines 1 - 10
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
... lines 14 - 18
}
... lines 20 - 28

This is Drupal's - and Symfony's - Request object. If you want to read some GET params, POST params, headers or session stuff, this is your friend. You can of course get the Request object inside a controller.

Here's the goal: if the URL has a ?roar=1 on it, then I want to log a message. If not, we'll do nothing. Make a new variable called $shouldRoar. To access the GET, or query parameters on the request, use $request->query->get('roar'):

28 lines modules/dino_roar/src/Jurassic/DinoListener.php
... lines 1 - 10
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$shouldRoar = $request->query->get('roar');
... lines 15 - 18
}
... lines 20 - 28

If it's not there, this returns null:

Next, if ($shouldRoar), just var_dump('ROOOOAR') and die to test things:

28 lines modules/dino_roar/src/Jurassic/DinoListener.php
... lines 1 - 10
public function onKernelRequest(GetResponseEvent $event)
{
... lines 13 - 15
if ($shouldRoar) {
var_dump('ROOOOOOOOAR');die;
}
}
... lines 20 - 28

Since we didn't touch any configuration, we can refresh without clearing anything.

Page not found! We're on the profiler page for a past request, and this information is stored in the cache... which we just cleared. So go find a real page. Ok, it works perfectly. Now add ?roar=1. It hits! And this will work on any page.

Dependency Inject All the Things

How can we log something? We faced this problem earlier when we wanted to use the keyvalue store inside RoarGenerator. We solved it with dependency injection: create a __construct() method, pass in what you need, and set it on a property. This is no different.

Add public function __construct(). Look for the logger in container:debug:

drupal container:debug | grep log

The logger.factory is an instance of LoggerChannelFactory. Type-hint using that. And like with other stuff, this has an interface, which is a trendier option. I'll hit option+enter to add the property and set it:

37 lines modules/dino_roar/src/Jurassic/DinoListener.php
... lines 1 - 4
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
... lines 6 - 9
class DinoListener implements EventSubscriberInterface
{
private $loggerChannel;
public function __construct(LoggerChannelFactoryInterface $loggerChannel)
{
$this->loggerChannel = $loggerChannel;
}
... lines 18 - 36
}

Below, if you wanna roar, do it! $this->loggerFactory->get() - and use the default channel. Then add a debug message: 'Roar requested: ROOOOOAR':

37 lines modules/dino_roar/src/Jurassic/DinoListener.php
... lines 1 - 9
class DinoListener implements EventSubscriberInterface
{
... lines 12 - 18
public function onKernelRequest(GetResponseEvent $event)
{
... lines 21 - 23
if ($shouldRoar) {
$channel = $this->loggerChannel->get('default')
->debug('ROOOOOOOOAR');
}
}
... lines 29 - 36
}

That's it guys! We didn't touch any config, so just refresh. Oh no, an error:

Argument 1 passed to DinoListener::__construct() must implement LoggerChannelFactoryInterface, none given.

Can you spot the problem? We saw this once before. We forgot to tell the container about the new argument. In dino_roar.services.yml, add @logger.factory:

16 lines modules/dino_roar/dino_roar.services.yml
... lines 1 - 3
services:
... lines 5 - 10
dino_roar.dino_listener:
... line 12
arguments: ['@logger.factory']
... lines 14 - 16

This is a single-line YAML-equivalent to what I did for the other service.

Rebuild the cache:

drupal cache:rebuild

Refresh. No error! Head into "Reports" and then "Recent Log Messages". There's the "Roar Requested".

You're now an event listener pro. We listened to the kernel.reqeust event, but you can add a listener to any event that Drupal or a third-party module exposes.

Leave a comment!

  • 2017-04-17 weaverryan

    That's totally possible - it's one of the tricky things about Drupal's container cache, which has bitten me a few times. With Drupal console, I believe it actually uses your Drupal container in order to do things. If your container is in a "broken" state, then you might not be able to rebuild it with drupal console. I don't know the specifics about this - I'm sure it's something they're aware of and there might be proper workarounds (though Drush is itself a very proper workaround!)

    Cheers!

  • 2017-04-17 joeyr33

    I think it's an issue with drupal console. If I try to load a page after adding the __constructor argument, but before doing drupal cache:rebuild then the the error Habibun Noby mentioned starts occuring. In fact, once that error gets thrown on page load it also prevents drupal cache:rebuild from running, but drush cr; works.

  • 2017-01-12 weaverryan

    Very happy it worked in the end - I thought your code looked perfect! :)

  • 2017-01-11 Habibun Noby

    hey weaverryan,

    i used drupal console for clearing cache.but some how it's not worked.So after that i manually delete all files under sites/default/files/* and rebuild again. now it's working....i don't know may be there is a issue with drupal console.

    your mentioned debug tricks help me to find out the problem....

    thanks

  • 2017-01-10 weaverryan

    Hey Habibun Noby!

    Hmm, interesting! So, your __construct() function (I assume this is from your DinoListener class?) and your services YAML code looks *perfect* to me: Drupal *should* be passing the LoggerChannelFactory into your DinoListener. So, let's debug!

    1) Have you definitely cleared all your caches? Of course, I always need to ask this first :).
    2) Do you have a full stack trace that you can take a screenshot of? I'm curious to see if possibly someone *else* is instantiating the DinoListener class, which is causing the confusion.

    But definitely, you're close - I see no issues at all with the code you have here.

    Cheers!

  • 2017-01-10 Habibun Noby

    I get same error message:
    TypeError: Argument 1 passed to Drupal\dino_roar\Jurassic\DinoListener::__construct() must implement interface Drupal\Core\Logger\LoggerChannelFactoryInterface, array given

      dino_roar.dino_listener:
    class: Drupal\dino_roar\Jurassic\DinoListener
    arguments: ['@logger.factory']
    tags:
    - { name: event_subscriber }

    myController:

        public function __construct(LoggerChannelFactoryInterface $loggerChannelFactory)
    {
    $this->loggerChannelFactory = $loggerChannelFactory;
    }