Aşağıdaki örnekte, mevcut olan bir adresi çağırıyoruz ve bu çağırı ile ilgili tüm detayları, onKernelController event listener ile log dosyasına yazıyoruz.


Listeners.yml


services:
application_backend.event_listener.kernel_controller:
class: Application\BackendBundle\EventListener\KernelControllerListener
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
arguments: [@logger]

KernelControllerListener.php


namespace Application\BackendBundle\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

class KernelControllerListener
{
private $logger;
private $log = [];

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

public function onKernelController(FilterControllerEvent $event)
{
$this->log['Event Name'] = $event->getName();
$this->log['Request Type V1'] = $event->isMasterRequest() ? 'Master' : 'Sub';
$this->log['Request Type V2'] = $event->getRequestType() ? 'Master' : 'Sub';
$this->log['Request Method'] = $event->getRequest()->getMethod();
$this->log['Request Format'] = $event->getRequest()->getRequestFormat();
$this->log['Base URL'] = $event->getRequest()->getBaseUrl();
$this->log['URI Path Info'] = $event->getRequest()->server->get('PATH_INFO');
$this->log['URI Query String'] = $event->getRequest()->getQueryString();
$this->log['Full URI'] = $event->getRequest()->getUri();
$this->log['Is Method Correct'] = $event->getRequest()->isMethod('GET') ? 'Correct method' : 'Incorrect method';
$this->log['Is URI Age Param Set'] = $event->getRequest()->attributes->get('age') ? 'Yes' : 'No';
$this->log['Controller'] = $event->getRequest()->attributes->get('_controller');
$this->log['Router'] = $event->getRequest()->attributes->get('_route');
$this->log['Client IP'] = $event->getRequest()->getClientIp();
$this->log['Router Parameters'] = json_encode($event->getRequest()->attributes->get('_route_params'));

$this->logger->info(json_encode($this->log));
}
}

Test


Bu adresi çağırıyoruz: http://football.local/app_dev.php/backend/user?page=1&limit=2


Alınan dev.log dosyasının içeriği


[2015-07-04 20:57:44] app.INFO: {"Event Name":"kernel.controller","Request Type V1":"Master","Request Type V2":"Master","Request Method":"GET","Request Format":"html","Base URL":"\/app_dev.php","URI Path Info":"\/backend\/user","URI Query String":"limit=2&page=1","Full URI":"http:\/\/football.local\/app_dev.php\/backend\/user?limit=2&page=1","Is Method Correct":"Correct method","Is URI Age Param Set":"No","Controller":"application_backend.controller.user:listAction","Router":"application_backend_user_list","Client IP":"127.0.0.1","Router Parameters":"[]"} [] []

Sonuç


{
"Event Name": "kernel.controller",
"Request Type V1": "Master",
"Request Type V2": "Master",
"Request Method": "GET",
"Request Format": "html",
"Base URL": "\/app_dev.php",
"URI Path Info": "\/backend\/user",
"URI Query String": "limit=2&page=1",
"Full URI": "http:\/\/football.local\/app_dev.php\/backend\/user?limit=2&page=1",
"Is Method Correct": "Correct method",
"Is URI Age Param Set": "No",
"Controller": "application_backend.controller.user:listAction",
"Router": "application_backend_user_list",
"Client IP": "127.0.0.1",
"Router Parameters": "[]"
}