Create page in Drupal programmatically
There are two steps to creating a simple page in Drupal:
Declare the route and its parameters.
This step includes the page title, access requirements, and more.
In Drupal 7, you had to implement hook_menu()
.
In Drupal 8, create a <module_name>.routing.yml
file in the top-level directory of your module.
Write the code that returns the page content.
In Drupal 7, you would write a page callback function specified in hook_menu()
.
In Drupal 8, the page callback should be either a class method or a registered service. It can vary based on different conditions (HTTP or HTTPS, content headers, etc.), but that’s beyond the scope of this introduction.
Walkthrough
By following the example on this page, you’ll be able to create a simple page in your custom module without needing to understand all the inner workings of Drupal. For more details, see the change record for the routing system and various implementations in core and example projects.
Routing YAML File for the Example Module
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 reference the route, they will use the machine name.
path
- This specifies the URL path to the page on your site. Note the leading slash (
/
).
defaults
- This section defines the page and title callbacks. @todo: Where can these defaults be overridden?
requirements
- This section defines the conditions under which the page will be displayed. You can specify permissions, required modules, and other criteria.
Page Implementation for the Example Module
The ExampleController
class must 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() { $element = array( '#markup' => 'Hello, world', ); return $element; } }
Namespace
- This declares the prefix required to fully define the class name. Compare the file path and class name. The class autoloader knows that to find the class
\Drupal\example\Controller\ExampleController
, it should look for the filemodules/example/src/Controller/ExampleController.php
.
use
- This allows us to use
ControllerBase
instead of writing the fully-qualified class name. It makes theclass
line much easier to read.
myPage()
- The method specified in the YAML file must be public and should return a renderable array.
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.