I'm implementing a per page cache. I'm minifying rendered HTML in a view:afterRenderView
event. When caching, this event is however not fired, so that the cached pages are not minified.
How do I get the cached pages to be minified?
I have things implemented like this:
The page caching happens in a class called from Controller->beforeExecuteRoute()
, and looks like this:
$pageCacheId = $sectionName.'-'.$pageAction.'.html';
$view->cache( ['key' => $pageCacheId] );
// Check for a cached version of this page
if ($view->getCache()->exists($pageCacheId)) {
return;
}
My minify code is in the services.php file, in $di->set("view", function () use ($config, $di) { .... }
:
// Minify after rendering view
$eventsManager = $di['eventsManager'];
$eventsManager->attach("view:afterRenderView", function($event, $view) {
$content = $view->getContent();
// Remove spaces at start of lines, and empty lines
$content = preg_replace('/\r?\n\s*(\r?\n)?/', "\n", $content);
// Remove xhtml endings
$content = str_replace(' >', '>', str_replace('/>', '>', $content));
$view->setContent($content);
});
$view->setEventsManager($eventsManager);
(I'm using Phalcon 2.0.9, Windows for development, Linux for production.)