Drupal 11: Scan All Routing Files For Paths

Use the following (stand alone) script for inspecting the yaml files inside a Drupal site.

<?php

require_once './vendor/autoload.php';

use Symfony\Component\Yaml\Yaml;

$directory = 'web/';

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::SELF_FIRST);
$objects = new RegexIterator($iterator, '/^.+\.routing\.yml$/i', RecursiveRegexIterator::GET_MATCH);

$pathsFound = [];

foreach ($objects as $name => $object) {
  $yamlFile = $object[0];
  $routes = Yaml::parseFile($yamlFile);
  if ($routes === NULL) {
    // Blank route file.
    continue;
  }
  foreach ($routes as $routeId => $routeInfo) {
    $pathsFound[$routeId] = $routeInfo['path'];
  }
}

print_r($pathsFound);

This assumes that the script is running from the same location as the composer.json file and that the webroot is contained in a directory called web.

I recently used this to troubleshoot an issue with broken configuration in a Drupal site. The script didn't find the error, but it was useful in that it ruled out the yaml files being the cause of the issue.

Scanning a fully installed site with around 50 modules takes less than a second.

This script can be adapted for any number of different situations where you need to inspect yaml files inside a Drupal install. Or, indeed, any application that uses yaml files for configuration. It will work on any Drupal 8+ site as they all have the same yaml structure.

Add new comment

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