logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

滚动
04/10/2025, by Ivan

返回到模块的根目录(即 .info.yml 文件所在的位置),添加一个名为 hello_world.routing.yml 的新文件,并在其中添加以下内容:

hello_world.content:
  path: '/hello'
  defaults:
    _controller: '\Drupal\hello_world\Controller\HelloController::content'
    _title: 'Hello World'
  requirements:
    _permission: 'access content'

请注意,你在路由表中为模块保留的位置——第一行中的 hello_world.content——不一定必须是你为模块选择的机器名称。然而,为了保证路由文件与菜单文件之间的一致性,这是最佳实践。完整的条目名称 hello_world.content 将在下一部分添加菜单链接时使用,以便将该链接与此路由表条目关联。

如果你的模块已经启用,你需要通过界面 admin/config/development/performance 或使用 drush(drush cache-rebuild 或 drush cr)来清除站点缓存。如果还没有启用,请继续启用它。

现在,转到你的网站首页,然后在网址后添加 /hello。你应该会看到一个显示 “Hello, World!” 信息的页面。

注意,module.routing.yml 文件中的缩进必须正确,否则在清除 drupal 缓存后可能会遇到问题。

了解更多内容,请参阅 Drupal 8 中的路由系统