Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

With the event listener below, we're going to see what actions cause master and sub requests throughout the system. I've listed some of them below but I guess this list can be extended more.


Request types


Master


External requests that directly come from the user action.



Sub


Internal requests that does not come from the user action.



Parameters.yml


parameters:
listener.log.file: '%kernel.logs_dir%/listener.log'

# football/app/logs/listener.log

Listeners.yml


services:
application_backend.listener.user_controller:
class: Application\BackendBundle\EventListener\UserControllerListener
arguments:
- %listener.log.file%
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

UserControllerListener.php


namespace Application\BackendBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class UserControllerListener
{
private $logFile;
private $message = [
true => 'Master request',
false => 'Sub request'
];

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

public function onKernelRequest(GetResponseEvent $event)
{
$this->logRequest(
$this->message[$event->isMasterRequest()],
$event->getRequest()->getMethod()
);
}

private function logRequest($message, $method = null)
{
file_put_contents(
$this->logFile,
file_exists($this->logFile)
? file_get_contents($this->logFile) . "\n" . $method . ':' . $message
: $method . ':' . $message
);
}
}

Listener.log


GET:Master request
GET:Sub request
GET:Master request
POST:Master request
GET:Master request
GET:Sub request
GET:Sub request
GET:Master request
GET:Master request
GET:Sub request
GET:Master request