在 Drupal 8 中重建缓存后丢失路由

Missing route after cache rebuild in Drupal 8

有一个名为 phys_custom 的模块,它定义了一个路由,phys_custom.homepage (/homepage),它工作正常,直到我清除缓存,此时它不再可用。

看看下面的输出。现在,每次清除缓存时,我都需要卸载并重新安装我的模块。我将如何调试它?

$ drush @stewardd8.test -l phys cr
[success] Cache rebuild complete.
$ drush @stewardd8.test -l phys pmu phys_custom
[success] Successfully uninstalled: phys_custom
$ drush @stewardd8.test -l phys pm:enable phys_custom
[success] Successfully enabled: phys_custom
$ drush @stewardd8.test -l phys ev 'print_r(drupal_get_path("module", "phys_custom") . PHP_EOL)'
sites/phys/modules/phys_custom
$ drush @stewardd8.test -l phys ev 'print_r(\Drupal\Core\Url::fromRoute("phys_custom.homepage")->toString() . PHP_EOL)'
/homepage
$ drush @stewardd8.test -l phys cr
[success] Cache rebuild complete.
$ drush @stewardd8.test -l phys ev 'print_r(drupal_get_path("module", "phys_custom") . PHP_EOL)'
sites/phys/modules/phys_custom
$ drush @stewardd8.test -l phys ev 'print_r(\Drupal\Core\Url::fromRoute("phys_custom.homepage")->toString() . PHP_EOL)'

In RouteProvider.php line 201:

 Route "phys_custom.homepage" does not exist.  

我正在使用 drush 9.6.2 和 Drupal 8.6.15

我也开了一个issue on Drupal.org

您命令中的 -l phys 部分可能是导致问题的原因。

Drush -l 选项的目的是针对多站点安装中的特定站点,它需要一个 URI :

-l <uri> , --uri=<uri> URI of the Drupal site to use. 
 The value of --uri should always be the same as when
 the site is being accessed from a web browser (e.g. http://example.com)

就是说,一旦别名 @stewardd8.test 定义明确,您就不必指定它,我的意思是至少这两个选项出现在相应的站点别名定义中:

test:
  root: /path/to/stewardd8
  uri: http://example.com

正确设置这些选项后,您可以 运行 drush status 并检查输出:

$ drush @stewardd8.test status
Drupal version                  :  8.6.14
Site URI                        :  http://example.com    # <- as expected

但是,如果您使用 -l phys 覆盖站点 URI,事情可能无法按预期工作:

$ drush @stewardd8.test -l phys status
Drupal version                  :  8.6.14
Site URI                        :  phys                  # <- invalid

所以我建议您在进一步挖掘之前开始解决这个问题。你也可以添加 -v, --verbose 选项使 drush 更冗长,例如drush -v @stewardd8.test cr.

然后,如果它不能修复丢失的路由问题,我会仔细检查 Drupal/PHP/SQL 日志(按此顺序),尤其是在 运行 执行缓存重建命令时。

这实际上是目前正在处理的一个核心错误,Extensions in Multisite Directories Not Registered When Rebuilding Cache 。那里的补丁解决了这个问题。