Create a custom page using a controller

Last updated on
3 April 2023

There are two steps in creating a simple page in Drupal. First, you need to declare the routing in <module_name>.routing.yml. Secondly, you need to add a controller that returns the page body in example/src/Controller/ExampleController.php.

Following the example on this page, you should be able to create a simple page in your custom module.

Declaring a route

The routing information is stored in example/example.routing.yml:

example.my_page:
  path: '/mypage/page'
  defaults:
    _controller: '\Drupal\example\Controller\ExampleController::myPage'
    _title: 'My first page in D8'
  requirements:
    _permission: 'access content'
example.my_page
This is the machine name of the route. By convention, route machine names should be module_name.sub_name. When other parts of the code need to refer to the route, they will use the machine name.
path
This gives the path to the page on your site. Note the leading slash (/).
defaults
This describes the page and title callbacks. @todo: Where can these defaults be overridden?
requirements
This specifies the conditions under which the page will be displayed. You can specify permissions, modules that must be enabled, and other conditions.

Add a controller (page callback)

The controller returns the page body. It must be either a class method or a registered service. It can be different depending on various conditions (HTTP vs. HTTPS, content headers, others) but that is beyond the scope of this introduction.

The Controller class ExampleController should be defined in example/src/Controller/ExampleController.php:

<?php
namespace Drupal\example\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Provides route responses for the Example module.
 */
class ExampleController extends ControllerBase {

  /**
   * Returns a simple page.
   *
   * @return array
   *   A simple renderable array.
   */
  public function myPage() {
    return [
      '#markup' => 'Hello, world',
    ];
  }

}
namespace
This declares the prefix needed to fully qualify the name of the class we are defining. Compare the file's doc block and the name of the class. The class auto-loader knows that, to find the class \Drupal\example\Controller\ExampleController, it should look for the file modules/example/src/Controller/ExampleController.php.
use
This allows us to use ControllerBase instead of the fully qualified name. This makes our class line much easier to read.
myPage()
The method specified in the YAML file must be public. It should return a renderable array.

Changes from Drupal 7

See the change record for changing the routing system.

Help improve this page

Page status: No known problems

You can: