Topic
Last updated August 17, 2017

When Drupal receives an incoming request the HTTP Kernel identifies the appropriate route for the requested path. The routing system then matches this route with a controller that has been registered to handle responses for these types of requests. Sections of code responsible for generating the response to these requests are called Controllers.

Example tasks

  • Write the code in a custom module that responds to particular incoming requests
  • Understand how to identify the controllers responsible for responding to a given url, path and route.

Confidence

Drupal's system of mapping request routes to controllers is quite well established. Since it's based on Symfony components it is unlikely to change dramatically in the foreseeable future.

Drupalize.Me resources

We are still filling out our Drupal 8 library and this page will be updated with new tutorials as they are created.

  • Symfony Controllers

    • This tutorial covers how controllers work in the Symfony framework. Because Drupal is built on top of Symfony components, controllers in Drupal are very similar.
  • Create a Module with a Route and Controller

    • This tutorial will walk you through creating a simple module with a custom route and controller.

More Guides

We have guides on many Drupal skills and topics.

Explore guides

External resources

  • Adding a basic controller (drupal.org)
    • This tutorial covers creating a very basic controller.
  • Routing and controllers in Drupal 8 (lakshminp.com)
    • This tutorial walks through creating a custom module, using Drupal Console, that creates a simple route and controller.
  • Examples' projects page_example module (cgit.drupalcode.org)
    • The Examples 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 page_example module here.