Created
January 29, 2013 21:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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