Drupal

How to Create a Basic “Hello World” Module in Drupal 10

πŸ‘‹ How to Create a Basic “Hello World” Module in Drupal 10


🟒 Step 1: Create the Module Folder

Navigate to the custom module directory in your Drupal installation:

cd web/modules/custom
mkdir hello_world
cd hello_world

πŸ—‚ Step 2: Create the .info.yml File

Create hello_world.info.yml β€” this file declares your module to Drupal.

name: Hello World
type: module
description: 'A basic Drupal 10 module that prints Hello World'
core_version_requirement: ^10
package: Custom

🧠 Step 3: Define a Route

Create a hello_world.routing.yml file:

hello_world.hello:
  path: '/hello'
  defaults:
    _controller: '\Drupal\hello_world\Controller\HelloWorldController::hello'
    _title: 'Hello'
  requirements:
    _permission: 'access content'

This defines a route available at /hello.


πŸ“ Step 4: Create the Controller

Create the controller class file at:

src/Controller/HelloWorldController.php

First, make the folder:

mkdir -p src/Controller

Now create the controller file:

<?php

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloWorldController extends ControllerBase {
  public function hello() {
    return [
      '#markup' => $this->t('Hello, World!'),
    ];
  }
}

βœ… ControllerBase gives access to translation and rendering functions.


πŸš€ Step 5: Enable Your Module

Use the Drupal Admin UI:

  1. Go to Extend (Admin β†’ Extend)
  2. Find “Hello World”
  3. Check it, and click Install

Or use Drush:

drush en hello_world
drush cr

βœ… Step 6: Visit the Page

Now go to:

https://your-site.local/hello

You should see:

Hello, World!


πŸ“ Summary

Here’s what we created:

File Purpose
hello_world.info.yml Registers the module
hello_world.routing.yml Adds a route for /hello
HelloWorldController.php Returns “Hello, World!” markup

Download from GITHUB

Leave a Reply

Your email address will not be published. Required fields are marked *