Skip to content

Instantly share code, notes, and snippets.

@jenkoian
Created January 29, 2013 21:40
Show Gist options
  • Save jenkoian/4668158 to your computer and use it in GitHub Desktop.
Save jenkoian/4668158 to your computer and use it in GitHub Desktop.
Symfony2 listener to change the controller action based on whether it implements Cacheable or not
<?php
namespace Acme\DemoBundle\Listener;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Acme\DemoBundle\Controller\Cacheable;
class CacheControllerListener
{
/**
* This method will check if the controller implements Cacheable, if it does, it will check the last modified
* date set on the controller and if the output is not modified will change the controller action
* to output getLastModifiedResponse instead of the action it would otherwise have called
*
* @param FilterControllerEvent $event
*/
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
if (!is_array($controller)) {
return;
}
$controllerObject = $controller[0];
if ($controllerObject instanceof Cacheable) {
if ($controllerObject->isNotModified($controllerObject->getLastModifiedDate())) {
$controller[1] = 'getLastModifiedResponse';
}
$event->setController($controller);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment