π 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:
- Go to
Extend
(Admin β Extend) - Find “Hello World”
- 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 |