Have you ever wanted to disable Drupal 8 cache and enable error reporting only for a specific page or to quickly debug a problem?
Here's a simple solution how to enable debugging and disable caching for a single call using a URL debug parameter
Some months ago I created a Drupal Core feature request issue for this topic, where you can read details: https://www.drupal.org/project/drupal/issues/2947316
Now I'd like to share my results and working solution with you.
Solution summary:
The solution was to use a snippet to attach some debug/development code in settings.php and this way also load a debug/development services.yml file if the secret URL parameter "&QUICKDEBUG=1" is present.
So if you call for example https://www.example.com/node/123?&QUICKDEBUG=1
this will disable all caches and enable error_reporting and display_errors.
This should of course NOT be used in production but is a helpful little trick for easy debugging without switching the whole environment status.
Solution code
At the end of your general settings.php you append the following code:
// ----------------------------- Error reporting: -----------------------------
///* -- Escape this line to enable error reporting inpage.
if (!empty($_REQUEST['QUICKDEBUG'])) {
error_reporting(-1); // Have PHP complain about absolutely everything.
$conf['error_level'] = 2; // Show all messages on your screen
ini_set('display_errors', TRUE); // These lines give you content on WSOD pages.
ini_set('display_startup_errors', TRUE);
// Load the disabled DEVELOPMENT SETTINGS for this QUICKDEBUG call:
if (file_exists($app_root . '/' . $site_path . '/__settings.QUICKDEBUG.php')) {
include_once $app_root . '/' . $site_path . '/__settings.QUICKDEBUG.php';
}
}
// */
?>
Which will set some php variables to display errors and appends __settings.QUICKDEBUG.php to override some settings.php variables.
__settings.QUICKDEBUG.php contains the variables you want to override for QUICKDEBUG. Furthermore it has to add our __services.QUICKDEBUG.yml file with this code:
if (file_exists($app_root . '/' . $site_path . '/__services.QUICKDEBUG.yml')) {
$settings['container_yamls'][] = $app_root . '/' . $site_path . '/__services.QUICKDEBUG.yml';
}
?>
We need to do this because the services.yml also sets some variables like twig cache, debugging and cache backend. For example the file may look like this:
parameters:
http.response.debug_cacheability_headers: true
twig.config:
debug: true
auto_reload: true
cache: false
services:
cache.backend.null:
class: Drupal\Core\Cache\NullBackendFactory
That's it. Place your custom
- __settings.QUICKDEBUG.php
- __services.QUICKDEBUG.yml
in your sites/default directory and have a happy debugging time :)