Routes

by Gisle Hannemyr

Mumble

Table of contents

Drupal project discussed in this chapter: Devel.

Introduction

Sources:

The Drupal routing system is the replacement in OO Drupal for legacy page callbacks to paths in hook_menu(). However, the routing system is not responsible for managing tabs, action links and contextual links.

Route vs path

Drupal 8 and later uses routes instead of paths.

See alsoIt is not intuitive that the path 'admin/structure/types' should be referred to by the route 'entity.node_type.collection'. For suggestions about getting route by means of the path, see Drupal.org: Get route name by path in Drupal 8.

Most routes are defined in the module's routing file. If you can guess what module that supplies the route, you can look it up. For example: The user/login page is provided by the core User module, so look it up in user.routing.yml. Here is the entry for user/login path:

user.login:
  path: '/user/login'
  defaults:
    _form: '\Drupal\user\Form\UserLoginForm'
    _title: 'Log in'
  requirements:
    _user_is_logged_in: 'FALSE'
  options:
    _maintenance_access: TRUE  

Getting the route from the path can be tricky. You may have to do a recursive grep for the path in the code.

The Web profiler ( part of the Devel project is a great tool for discovering route names.

Routes may include placeholder elements which designate places where the URL contains dynamic values. In the controller method, this value is available when a variable with the same name is used in the controller method. For example in mymodule.routing.yml:

mymodule.name:
  path: '/mymodule/{name}'
  defaults:
    _controller: '\Drupal\mymodule\Controller\MymoduleController::content'
  requirements:
    _permission: 'access content'

The {name} element in the URL is called a slug and is available as a $name in the controller method.

Route to a specific node

https://drupal.stackexchange.com/q/305585/12076

Final word

[TBA]


Last update: 2021-08-11 [gh].