Member-only story
Ensuring Drupal route lookups by path are cached by domain
In this episode of “Matt does ridiculous things with Drupal,” I found out that the inbound processing of a path and the lookup for its route gets cached. That makes sense, it can be an expensive process to say /foo
is actually an alias of /node/1
and should go to the entity view controller. What I did not know is that the route provider in Drupal creates a cache ID based on the path and language code, only.
Here’s where the cache ID for the request’s route collection is generated (link to the full source.)
protected function getRouteCollectionCacheId(Request $request) {
// Include the current language code in the cache identifier as
// the language information can be elsewhere than in the path, for example
// based on the domain.
$this->addExtraCacheKeyPart('language', $this->getCurrentLanguageCacheIdPart());
// Sort the cache key parts by their provider in order to have predictable
// cache keys.
ksort($this->extraCacheKeyParts);
$key_parts = [];
foreach ($this->extraCacheKeyParts as $provider => $key_part) {
$key_parts[] = '[' . $provider . ']=' . $key_part;
}
return 'route:' . implode(':', $key_parts) . ':' . $request->getPathInfo() . ':' . $request->getQueryString();
}
That means if you have two domains pointed at the same Drupal site and want to process an inbound path differently, you cannot. Out of the box, that is. However, there is a bit of an escape hatch. The route provider doesn’t use cache…