This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

✨ Vultr and Linode are back on Cloudways! VIEW PLANS→

How to Create a Module in Drupal 8

Updated on February 19, 2018

4 Min Read

The biggest trait of CMSs like Drupal is their ability to create websites without the need for users to delve into coding. However, the real power is unleashed when you get to make your own custom modules and customize them according to your needs.

Keeping that in mind previously I presented tutorial for creating Drupal 8 module via Drupal console , but today I’ll show you how to make your own custom module for Drupal’s latest version i.e Drupal 8. So without further ado, let’s get started. Here’s a list of all the essentials that you will need to follow:

Name Your Module

The very first thing that you need to do is to decide a name for your module. In Drupal, we give our module a machine name. Drupal will identify your module by this name. Note that this name is different from the name that is seen by the user. Here are some of the things that you should keep in mind before giving a machine name to your module:

  • It shouldn’t start with uppercase letters.
  • There shouldn’t be spaces between the words.

Since we all started programming using the famous Hello World, I’ll be naming our first custom module hello world as well. The machine name that I’ll give it to is hello_world.

Place Module in Drupal

We will need to create a new directory in Drupal where we’ll place our module. Although you can place this new module directly in the Drupal root’s module directory, it is always better to place it in sites/all/modules folder in order to be able to differentiate between Drupal’s core modules and our custom modules. So in order to do this, create a new directory under the sites directory as shown.

site structure

Create a folder in this directory with the machine name of your module. (In this case hello_world)

Nothing as Easy as Deploying Drupal Apps on Cloud

With Cloudways, you can have your PHP apps up and running on managed cloud servers in just a few minutes.

Create Module’s yaml File

In Drupal 8 all .info files have been replaced to yaml files with the extension .yml . For Drupal to be able recognize the module, we have to create a yaml file with the machine name of our module. So I’ll create a yaml file like this . Inside this file, we will include details of the module so that Drupal can identify it:

name: Hello World (The name to be displayed on the modules list in Drupal)

description: First Custom Drupal 8 Module (Description of the module to be displayed)

package: Custom - (Declaring that this is a custom module)

type: module - (Declaring that this is a module)

version: 1.0 - (Version of the module)

core: 8.x - (The version of Drupal)

Create Routing file

Now we will create a routing file for the module. This file will be used by the controller to know at which URL path the module’s function will execute. The name of our file will be hello_world.routing.yml . Inside this file, the following code will be placed:


path: /hello/world  - (The url path)  


_controller: Drupal\hello_world\Controller\HelloWorldController::hello  - (the namespace and the method that the controller will call)  


_permission: 'access content' - (The permissions required for the module to successfully perform its function)

Create Directory for Controller

Next, we will need to create a new subdirectory under our module’s folder which will hold the controller file. All external plugins, controllers etc are placed under this directory. We name it src short for source. Create a folder named src under your module’s root directory, then create a new folder under src with the name Controller .

controller structure

Create Controller File

We will now create a controller file which essentially controls the functionality of your module. Make sure that the name is relatable so that it’s easily identified. In my case, I’ll give it the name HelloWorldController.php . In this file enter the following code:


namespace Drupal\hello_world\Controller;

class HelloWorldController {

public function hello() {

return array(

'#title' => 'Hello World!',

'#markup' => 'Content for Hello World.'


Check If the Module Functions

Login to your Drupal site and enable your module, if you haven’t done it already. To check if it functions properly, visit the path you specified in your routing file. If you get the ‘Page Not Found’ error, then clear the cache by navigating to admin->configuration->performance . Check again and it should function properly this time.

Unleash Drupal Excellence with Cloudways’ Hosting Solutions

Experience the future of Drupal hosting with Cloudways – where reliability and speed converge for your web projects.


That’s it! You’ve successfully created your very first custom Drupal 8 module. Although this is a very simple Drupal module, it essentially covers all the basics of creating a Drupal 8 module. If you encounter any issues, even if it’s related to Drupal hosting, get in touch with me in the comments section below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Hamza Zia

Hamza is a Drupal Community Manager at Cloudways - A Managed Drupal Hosting Platform. He loves to write about Drupal and related topics. During his free time, he can be seen obsessing over Football, Cars, Android and Gaming.


Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour


  • 0


  • 0


  • 0


  • 0



For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now