Drupal 8 hello world module

A simple tutorial for creating a Hello World module on Drupal 8.
Our goal here is to have two pages (/hello and /hello/{name}): one will show the “Hello World!” message and the other one will show a “Hello visitor name” message using the {name} parameter from the url.

First, you should have a local Drupal 8 installation. The folder structure is:

drupal 8 folder structure

We will create our module in the ‘/modules’ folder (the new prefered location for custom modules in Drupal 8) and name it ‘hello_world’.

drupal 8 hello world module

Similar to Drupal 7, we’ll let Drupal know of our module by creating the ‘.info.yml’ and the ‘.module’ files, the difference here beign that the ‘.info.yml’ file uses the YAML format (YAML is a human friendly data serialization standard for all programming languages)

drupal 8 hello world module expanded

hello_world.info.yml

name: Hello World
description: 'Display a hello world page and a greetings page'
type: module
package: Examples
core: 8.x

In the first 2 lines we are telling Drupal the name and the description of our module (keys: name and description).
The extension type is set to module (key: type), the module is part of the “Examples” package (key: package) and our module is made to work with Drupal 8 (key: core).

hello_world.module

<?php
/**
* @file
* Module file for hello_world_module.
*/

The hello_world.module will have just a description in our example and nothing more.

Before continuing, you should know some new things about Drupal 8 related to our example.
Drupal 8 uses Object Oriented Programming (OOP) approach to most parts of the system, that means classes will be used for our code.
The classes will be autoloaded using the PSR-4 standard and we’ll have specific rules for the namespaces and folder structures.

Each module’s namespace is mapped to a “src” folder, so we’ll need create that folder:

drupal 8 hello world module

Routes and controllers

Drupal 8 does the routing by using the Symfony HTTP Kernel (you won’t need any extra knowledge about it for our example).
The routing can be simplified to something like this : Request -> Controller -> Response.
That means the code that process the request and give the response will be located inside the Controller classes.

Let’s create now the code that will generate the content for our two pages, /hello and /hello/{name}.
For this we will need to create the Controller class, we’ll name it HelloWorldController and place it inside a Controller folder.

drupal 8 hello world complete

HelloWorldController.php

<?php

/**
 * @file
 * Contains \Drupal\hello_world\Controller\HelloWorldController.
 */

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Controller for displaying our example pages. 
 */
class HelloWorldController extends ControllerBase {

 /**
 * Create the content for the hello world page.
 */
 public function showHelloWorldMessage() {
 return array(
 '#type' => 'markup',
 '#markup' => $this->t('Hello World!'),
 );
 }

 /**
 * Create the content for greeting the visitor.
 *
 * @param string $name
 * The name of the visitor, from the route parameter.
 */
 public function greetVisitor($name) {
 return array(
 '#type' => 'markup',
 '#markup' => $this->t('Hello @name!', array('@name' => $name)),
 );
 }

}

Every controller that we create must inherit from the ControllerBase class.
Our HelloWorldController class has two functions: showHelloWorldMessage for showing a simple markup containing the Hello World message and greetVisitor that will show a Hello {name} message  using the parameter provided.

Next we must create the routing file, hello_world.routing.yml.

hello_world.routing.yml

# Our example routes, for hello world and for greetings

# The route to show the hello world page
hello_world_no_arguments:
 path: '/hello'
 defaults:
 _controller: '\Drupal\hello_world\Controller\HelloWorldController:showHelloWorldMessage'
 _title: 'Hello World'
 requirements:
 _access: 'TRUE'

# The route to show the greeetings page
hello_world_with_arguments:
 path: '/hello/{name}'
 defaults:
 _controller: '\Drupal\hello_world\Controller\HelloWorldController:greetVisitor'
 _title: 'Hello'
 requirements:
 _access: 'TRUE'

The file is using the YAML standard and has the two routes, the last having arguments.
Some details here for the routes configuration keys:

hello_world_no_arguments is the machine name of the route
path (required) is the page url
defaults (required) provide the default properties of a route
_controller (required in our example) specify the controller to be used for this route
_title (optional) sets the page title
requirements (required) specify the conditions that must be met in order to grant access to the route
_access is set to TRUE to allow access for all to this route

We have now all the files for our example module, all what remains is that you enable the module and visit the two pages:
/hello and /hello/John for example.
If you have any issues or do any changes to the code, be sure to clear the Drupal cache too.
You can also download the module here.