Module Development
Topic

Routing for Drupal 8, 9, and 10

When a user visits a particular URL on a Drupal site, the path they are visiting is a route. Drupal's routing system helps figure out which controller is responsible for responding to the route being requested. Drupal matches the incoming request's path to a controller by looking through the routes that have been registered in active modules.

Example tasks

  • Create your own routes in custom modules
  • Understand how Drupal's routing system maps paths to controllers

Confidence

At this point in the development cycle of Drupal the routing system is relatively stable. This is mainly because Drupal's routing system leverages the Symfony routing component.

Drupalize.Me resources

Posted on Tuesday, January 30, 2024 - 21:04 by Blake Hall

In this installment of our series on Symfony's role in Drupal, we're focusing on the Routing component. Even if it may seem simple looking from the outside, routing in Drupal is a complex task with lots of customized parts. The routing component's job is to match incoming requests to the correct controller, which is then responsible for building the response. Let's take a high-level glance at how Drupal has built upon Symfony's Routing component.

Drupal 8, 9, and 10
More information

Before you can create a path or link to another page on your site, you'll need to know the route (unless there is already a variable available for the URL you need). Finding a route can be a tricky task unless you have the right tools. In this tutorial, we'll show how tools like Webprofiler, Drush, and Grep can be used to get route information for a page, so that you can use functions that need a route as a function parameter.

In this tutorial we'll:

  • Learn how to determine the route or path of an internal page.
More information

Sometimes we need the response returned from a controller to be something other than HTML content wrapped with the rest of a Drupal theme. Maybe we need to return plain text, or JSON structured data for an application to consume. Perhaps we need greater control over the HTTP headers sent in the response. This is possible by building on the fact that controllers can return generic Response objects instead of renderable arrays, allowing you to gain complete control over what is sent to the requesting agent.

In this tutorial we'll:

  • Look at how to return a plain text response, and JSON data
  • Show how to make your responses cacheable by adding cacheability metadata
  • Learn about how to use a generic Symfony Response to gain greater control over what gets returned

By the end of this tutorial, you should be able to return responses from a controller in a Drupal module that are not HTML content wrapped in a Drupal theme.

Categories
Drupal 8, 9, and 10
More information

In this tutorial we will learn how to add menu links using a custom Drupal module. We will also look at the options available for configuring our new menu link. Lastly, we'll learn about using hook_menu_links_discovered_alter() that can be used to add new menu links, or alter the behavior of existing ones.

More information

Each form is defined by a controller, a class that implements the \Drupal\Core\Form\FormInterface. Form controllers declare the unique ID of the form, the $form array that describes the content of the form, how to validate the form, and what to do with the data collected.

In this tutorial we'll:

  • Define a new form controller class
  • Implement the required methods to describe a form
  • Add a route that can be used to access our form

By the end of this tutorial you should be able to define a form that adheres to the FormInterface requirements and know where to find more information about how to further customize your form controller.

This tutorial walks through the process of creating a custom content entity. Since there are several url paths that will display various types of information about our custom entity there are multiple examples of registering routes in this tutorial as well.

Categories
Drupal 8, 9, and 10
More information

The Drupal Entity API makes it easy to define new custom content entity types. And in most cases whenever you want to store custom content in Drupal you should use the Entity API instead of making CRUD queries directly to the database. This ensures your code is more consistent, and allows you to take advantage of all the features of the Entity API like access control, Views module integration, and automatic JSON:API support. As well as making it easier for others to extend your custom content by ensuring all the proper hooks and lifecycle events get invoked when new content items get created, updated, and deleted.

In this tutorial we'll:

  • Walk through the process of creating a custom content entity

By the end of this lesson you'll be able to create your own custom content entity contained in a module.

Related topics

Categories
Drupal 8, 9, and 10
More information

Controllers are the portion of a Drupal module that handle responding to an incoming request with the appropriate response data.

Menus

Topic
Drupal 7, 8, 9, and 10
More information

A site’s main navigation elements, whether they be in the header, footer, or sidebar, are composed of links built using Drupal's menu system. Drupal allows site administrators to build menus while also allowing module developers to add and alter menus in code.

Symfony

Topic
Categories
Drupal 8, 9, 10, and 11
More information

Symfony is a set of reusable PHP components, and a framework for building PHP applications. Drupal makes use of various Symfony components.

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate guides

External resources

  • Routing system overview (Drupal.org)
    • The main entry point for Drupal's official documentation about the routing system.
  • Symfony's routing component (symfony.com)
    • The official documentation for the Symfony routing component that Drupal's routing system is based on.
  • Structure of routes (Drupal.org)
    • This page in particular covers the variety of configuration options that make up routing configuration files.
  • Examples projects page_example module (git.drupalcode.org)
    • The Examples for Developers project contains many sub-modules that demonstrate various Drupal sub-systems through well-documented code. To learn more about how routing and controllers work, take a look at the Examples for Developers project's page_example module.