---
title: "Introduction to Drupal 8 Module Development · Appnovation"
url: "https://www.appnovation.com/insights/introduction-drupal-8-module-development"
description: "Drupal 8's architecture differs from Drupal 7. An introduction to module development with Symfony components, routing and services."
---

Article

# Introduction to Drupal 8 Module Development

[![](https://cdn.sanity.io/images/ey850001/production/68ea411feeb79ffb29cb1fe2d06f74995dc6351d-68x68.jpg?w=68&auto=format) Jerome Jerome Drupal Developer](/people/jerome-jerome.md) Apr 04, 2016  
 4 min read 

![Introduction to Drupal 8 Module Development](https://cdn.sanity.io/images/ey850001/production/fe8817ce0702cbe13324bfa699957aaad4e6708a-1400x400.jpg?w=1400&auto=format) 

**Introduction**

Drupal 8 philosophy and architecture is different than Drupal 7. From a backend and frontend standpoints there are new concepts. One of the new things is the adoption of certain components of Symfony framework and some external libraries such as Doctrine, Assetic, Backbone, Guzzle, Twig, etc. The MVC (Model View Controller) architecture is used and it's one of the important improvements of the drupal platform.

**Drupal 8 Routing system** 

A route is a path defined for Drupal to return a page (content). For example, the page , '/blog' from the Appnovation website is a route. When Drupal receives a request, it tries to match the requested path to a defined and known route. If the route is not defined of course we'll see a "beautiful" 404 error page. On Drupal 7 routing parts of hook\_menu() were used for creating menu entries, tabs, contextual links...

Below is a good description of how Drupal 8 processes a request: 

**Let's create our first module**

Inside the "/sites/all" directory create a "modules" directory. Inside this last directory, create a directory named "appno\_custom", this is the name of our module. We need to tell Drupal that the module exists, on D8 it's done on a .yml file. Here is a link to this markup languguage ([yml (opens in new window)](https://www.drupal.org/node/1935708)).

Create "**appno\_custom.info.yml**" file inside the "**appno\_custom**" directory.

**This is the structure of the file.**

*name: Appno custom D8 module*

*description: A custom module to show how D8 works*

*package: Custom*

*type: module*

*version: 1.0*

*core: 8.x*

**The .module file**

Put appno\_custom.module in the root of the **appno\_custom** directory.

**The src directory**

We have to create a directory that will contain controllers, forms, plugins, tests...It should be called src, with this the controller class will be "autoloaded".

Inside "**appno\_custom**" folder create the src subdirectory.

**The controller**

Here we'll create a simple controller. In [MVC (opens in new window)](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/MVC-Process.svg/2000px-MVC-Process.svg.png) applications, the controller do most of the work. Create a "Controller" directory inside the src one. Now let's create the controller file inside the "Controller" folder an name it CustomController.php. We will just return a text.

/\*\* \* @file \* Contains \\Drupal\\appno\_custom\\Controller\\CustomController.php \*/ **namespace** Drupal\\appno\_custom\\Controller;

**use** Drupal\\Core\\Controller\\ControllerBase;

class CustomController **extends** ControllerBase {

public function content() {

return array( '#type' => 'markup', '#markup' => t('Hi Appnovators'), ); } }

// Comments on this code

You may see some unusual keywords on a drupal module development ^^.

Yeah as I said the D8 philosophy is different, it's an Orieted Object approach. Let's take a look at the three words in bold.

[Namespace (opens in new window)](http://php.net/manual/en/language.namespaces.basics.php) are commonly structured as hierarchies to allow reuse of names in different contexts. For example in ths file all functions(methods) that we will declare will be used on the context "Drupal\\appno\_custom\\Controller".

The second keyword allow to "include" an other class. That means that we wi'll be able to call methods from this class.

The last one is extends. Inheritance is one of the most important concept of the OOP. Here this instruction means that our Controller have "ControllerBase" class as parent. Extends involve a hierarchy relationship between classes.

A [simple schema (opens in new window)](http://www3.ntu.edu.sg/home/ehchua/programming/java/images/OOP_UMLSuperSubClass.png) to understand relationship between two classes and more details about [inheritance (opens in new window)](https://en.wikipedia.org/wiki/Inheritance_\(object-oriented_programming\)) in OOP.

**The route**

The controller that we created does nothing at this point. We need to associate a route to the controller so that it can be executed.

Create a file called appno\_custom.routing.yml at the root of appno\_custom folder and add the following code.

appno\_custom.content:

path: '**custom**'

defaults:

\_controller: 'Drupal\\appno\_custom\\Controller\\CustomController

\_title: 'Hi Appnovators'

requirements:

\_permission: 'acces content'

The word in bold on the code is thename of the controller without the "controller" suffix and in lowercase.

In order to view the content go to the '/custom' page, the 'Hi appnovators" message is returned.

Now that the route works and we have some markup returned, we can create a link to go directly to this page when clicking it.

Create an **appno\_custom.links.menu.yml** file in the module root and add the following.

appno\_custom.admin:

title: 'Appno custom module settings'

description: 'A sample drupal 8 module'

parent: system.admin\_config\_development

route\_name: first\_module.content

weight: 100

We define here the admin settings for our module. Clear the cache and then go to configuration > development. You should see the menu item and by clicking on it you will go to the '/custom' page. We have our first Drupal8 module.

We can add custom blocks and other plugins to the module, will explain how to create blocks for Drupal 8 on an other post!

[![](https://cdn.sanity.io/images/ey850001/production/68ea411feeb79ffb29cb1fe2d06f74995dc6351d-68x68.jpg?w=68&auto=format) Jerome Jerome Drupal Developer · Appnovation](/people/jerome-jerome.md) 

## Keep reading

[![Abstract graphic of white lines spiralling around a green-outlined triangle on a dark background](https://cdn.sanity.io/images/ey850001/production/e561f763d9608b75872d9b71d0674353c36a06fb-1201x630.png?w=640&auto=format)](/insights/2020-11-your-tech-stack-standing-way-your-success.md)

 Article · 4 min Nov 19, 2020 

### [Is Your Tech Stack Standing In The Way Of Your Success?](/insights/2020-11-your-tech-stack-standing-way-your-success.md)

Your business potential and technology needs (should) go hand in hand. You can plan business outcomes all you want, but if they’re not mapped strategically to the technology you need to actually make them happen, you’ll fail. Easy as that.

[Nathan Gervais](/people/nathan-gervais.md) Article 

[![Abstract graphic of white lines spiralling around a green-outlined triangle on a dark background](https://cdn.sanity.io/images/ey850001/production/e561f763d9608b75872d9b71d0674353c36a06fb-1201x630.png?w=640&auto=format)](/insights/2020-11-finding-your-way-build-your-migration-roadmap-drupal-9.md)

 Article · 4 min Nov 05, 2020 

### [Finding Your Way: Build Your Migration Roadmap to Drupal 9](/insights/2020-11-finding-your-way-build-your-migration-roadmap-drupal-9.md)

As we move toward 2021 and continue to reckon with COVID-19, digital-first companies need a plan to meet continuously shifting customer needs. Drupal 9 will help you do it.

[Nathan Gervais](/people/nathan-gervais.md) Article 

[![Abstract graphic of white lines spiralling around a green-outlined triangle on a dark background](https://cdn.sanity.io/images/ey850001/production/e561f763d9608b75872d9b71d0674353c36a06fb-1201x630.png?w=640&auto=format)](/insights/2020-10-move-drupal-9-and-exceed-cxpectations.md)

 Article · 5 min Oct 20, 2020 

### [Move to Drupal 9 and Exceed CXpectations](/insights/2020-10-move-drupal-9-and-exceed-cxpectations.md)

As we move toward 2021 and continue to reckon with COVID-19, digital-first companies need a plan to meet continuously shifting customer needs. Drupal 9 will help you do it.

[Nathan Gervais](/people/nathan-gervais.md) Article 

## Get the thinking,  
*before everyone else.*

One considered email a month. New essays, field notes from live engagements, and the occasional strong opinion. No noise.
