I'm really struggling to understand this and now I'm just going round in circles.
I've read as much of the manual as possible, paid for a video tutorial, scoured Google and YouTube and just can't get this working.
I am simply trying to set up a listener that activates before every request. I can do this, but my problem is getting access to the various other parts I need.
Below is an example but I think only actual code will help me understand this now.
I would appreciate if anyone could fill in the blanks. It's just an example, but each part will explain to me what it is I need to know.
In config.yml:
services:
kernel.listener.request_listener:
class: AcmeBundleNewBundleEventListenerRequestListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
arguments: [ '@service_container' ]
The class:
namespace AcmeBundleNewBundleEventListener;
use SymfonyComponentHttpKernelEventGetResponseEvent;
***do I need to 'use' any others here?***
class RequestListener
{
public function onKernelRequest($container) {
//reference to these: http://api.symfony.com/2.1/Symfony/Component/HttpKernel/Event/KernelEvent.html
$kernel =
//reference to the Request object
$request = $kernel->getRequest();
//reference to the Response object
$response =
//options:
// (1) continue to run usual content
// (2) stop execution and output a message
// (3) set cookie and continue to run usual content
switch( $request->query->get('option') ) {
case 1:
return
case 2:
$this->setResponse("hello, message here");
break;
case 3:
// *** not sure if this is the way to do it ***
$response->headers->setCookie(new Cookie("test", 1));
break;
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…