Drupal 8 on-demand error display with disabled cache via URL Parameter

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:

  1. // ----------------------------- Error reporting: -----------------------------
  2. ///* -- Escape this line to enable error reporting inpage.
  3. if (!empty($_REQUEST['QUICKDEBUG'])) {
  4.   error_reporting(-1);  // Have PHP complain about absolutely everything.
  5.   $conf['error_level'] = 2;  // Show all messages on your screen
  6.   ini_set('display_errors', TRUE);  // These lines give you content on WSOD pages.
  7.   ini_set('display_startup_errors', TRUE);
  8.  
  9.   // Load the disabled DEVELOPMENT SETTINGS for this QUICKDEBUG call:
  10.   if (file_exists($app_root . '/' . $site_path . '/__settings.QUICKDEBUG.php')) {
  11.     include_once $app_root . '/' . $site_path . '/__settings.QUICKDEBUG.php';
  12.   }
  13. }
  14. // */

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:

  1. if (file_exists($app_root . '/' . $site_path . '/__services.QUICKDEBUG.yml')) {
  2.   $settings['container_yamls'][] = $app_root . '/' . $site_path . '/__services.QUICKDEBUG.yml';
  3. }

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 :)

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.

More information about formatting options