The above works as expected on all controllers that extend ContentController, unless they have defined their own index() method. For example the BlogController from silverstripe/blog module.
Question: How can I augment the ContentController->index() method so it is not replaced by other modules?
Feel free to tell me if this is not the correct way to go about minification with SS!
You might be better off using a middleware for this, rather than doing it in a controller. That way you can just catch the full output of all the controllers, etc. without worrying what might override your code:
For others that may end up here looking for an HTML minification solution, here’s the the code I ended up using:
<?php
namespace myNamespace;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Control\HTTPRequest;
class HTMLMinificationMiddleware implements HTTPMiddleware
{
public function process(HTTPRequest $request, callable $delegate)
{
$response = $delegate($request);
$responseBody = $response->getBody();
// you may want to filter out requests to admin pages here and just return the unmodifed response
// return $response;
//apply your chosen minification method here - here is a basic preg_replace to get started
$responseBody_Minified = preg_replace(array('/<!--(.*)-->/Uis',"/[[:blank:]]+/"),array('',' '),str_replace(array("\n","\r","\t"),'', $responseBody));
return $response->setBody($responseBody_Minified);
}
}