Modules, Routes and Controllers

Creating a Module

Let's do something fun, like create a custom page. Like always, any custom code will live in a module. And modules live in the modules/ directory. Create a new one called dino_roar. To make Drupal fall in love with your module, create the info file: If you loved the old .info files, then you'll feel all warm and fuzzy with these: it's the same thing, but now in the YAML format.

Inside give it a name: Dino ROAR, a type: module, description: Roar at you, package: Sample and core: 8.x:

6 lines modules/dino_roar/
name: Dino ROAR
type: module
description: "ROOOOOAR at you"
package: Sample
core: 8.x

If YAML is new to you, cool! It's pretty underwhelming: just a colon separated key-value pair. But make sure you have at least one space after the colon. Yaml also supports hierarchies of data via indentation - but there's none of that in this file.

Module ready! Head back to the browser and go into the "Extend" section. With any luck we'll see the module here. There it is under "Sample": "Dino ROAR". It sounds terrifying. Check the box and press the install button anyways. What's the worst that could happen?

Nothing! But now we can build that page I keep talking about.

Add a Route

In any modern framework - and I am including Drupal in this category, yay! - creating a page is two steps. First, define the URL for the page via a route. That's your first buzzword in case you're writing things down.

Second, create a controller for that page. This is a function that you'll write that actually builds the page. It's also another buzzword: controller.

If these are new buzzwords for you, that's ok - they're just a new spin on some old ideas.

For step 1, create a new file in the module: dino_roar.routing.yml. Create a new route called dino_says: this is the internal name of the route and it isn't important yet:

7 lines modules/dino_roar/dino_roar.routing.yml
... lines 2 - 7

Go in 4 spaces - or 2 spaces, it doesn't matter, just be consistent - and add a new property to this route called path. Set it to /the/dino/says: the URL to the new page:

7 lines modules/dino_roar/dino_roar.routing.yml
path: /the/dino/says
... lines 3 - 7

Below path, a few more route properties are needed. The first, is defaults, with a _controller key beneath it:

7 lines modules/dino_roar/dino_roar.routing.yml
path: /the/dino/says
_controller: Drupal\dino_roar\Controller\RoarController::roar
... lines 5 - 7

The _controller key tells Drupal which function should be called when someone goes to the URL for this exciting page. Set this to Drupal\dino_roar\Controller\RoarController::roar. This is a namespaced class followed by :: and then a method name. We'll create this function in a second.

Also add a requirements key with a _permission key set to access content:

7 lines modules/dino_roar/dino_roar.routing.yml
... lines 2 - 4
_permission: 'access content'

We won't talk about permissions now, but this is what will allow us to view the page.

In YAML, you usually don't need quotes, except in some edge cases with special characters. But it's always safe to surround values with quotes. So if you're in doubt, use quotes! I don't need them around access content... but it makes me fee good.

Add a Controller Function

Step 1 complete: we have a route. For Step 2, we need to create the controller: the function that will actually build the page. Inside of the dino_roar module create an src directory and then a Controller directory inside of that. Finally, add a new PHP class called RoarController:

14 lines modules/dino_roar/src/Controller/RoarController.php
... lines 2 - 14

Ok, stop! Fun fact: every class you create will have a namespace at the top. If you're not comfortable with namespaces, they're really easy. So easy that we teach them to you in 120 seconds in our namespaces tutorial. So pause this video, check that out and then everything we're about to do will seem much more awesome.

But you can't just set the namespace to any old thing: there are rules. It must start with Drupal\, then the name of the module - dino_roar\, then whatever directory or directories this file lives in after src/. This class lives in Controller. Your class name also has to match the filename, + .php:

14 lines modules/dino_roar/src/Controller/RoarController.php
namespace Drupal\dino_roar\Controller;
... lines 4 - 6
class RoarController
... lines 9 - 12

If you mess any of this up, Drupal isn't going to be able to find your class.

The full class name is now Drupal\dino_roar\Controller\RoarController. Hey, this conveniently matches the _controller of our route!

In RoarController, add the new public function roar():

14 lines modules/dino_roar/src/Controller/RoarController.php
... lines 1 - 6
class RoarController
public function roar()
... line 11

A Controller Returns a Response

Now, you might be asking yourself what a controller function like this should return. And to that I say - excellent question! Brilliant! A controller should always return a Symfony Response object. Ok, that's not 100% true - but let me lie for just a little bit longer.


The code-styling (4 spces indentation, etc) I'm using is called PSR-4. It's a great PHP standard, but is (I admit) different than the recommended Drupal standard.

To return a response, say return new Response(). I'll let it autocomplete the Response class from Symfony's HttpFoundation namespace. When I hit tab to select this, PhpStorm adds the use statement to the top of the class automatically:

14 lines modules/dino_roar/src/Controller/RoarController.php
... lines 1 - 4
use Symfony\Component\HttpFoundation\Response;
class RoarController
public function roar()
return new Response('ROOOOOAR!');

That's important: whenever you reference a class, you must add a use statement for it. If you forget, you'll get the famous "Class Not Found" error.

For the page content, we will of course ROOOAR!.

That's it! That's everything. Go to your browser and head to /the/dino/says:


Hmm page not found. As a seasoned Drupal developer, you may be wondering, "uhh do I need to clear some cache?" My gosh, you're right!

Leave a comment!

  • 2017-01-09 weaverryan

    Awesome :). If you make some progress, I'd love to hear about it!

  • 2017-01-09 Matt Holbrook-Bull

    Cool, I had a little play about over the weekend but ran out of time. I'm going to give it a try, out of pure bloody mindedness if nothing else!

    I'll keep you posted.



  • 2017-01-08 weaverryan

    Yo Matt!

    As far as I can tell... surprisingly... this doesn't seem to be supported, or even too easily added into the core of Drupal! Part of the problem is that, in Symfony, some of the @Route functionality comes from SensioFrameworkExtraBundle - i.e. a *Bundle*... not actually from the core of Symfony, which is shared by Drupal.

    So... it depends on how you define "without too much pain" :). And actually, it would be kind of fun to try out. Here is how (I think) it would work:

    1) Obviously, start by adding some annotations to a controller. Use this class for the use statement:

    2) Create a subscriber on RoutingEvents::ALTER - as described here:

    3) In alterRoutes, instead of altering routes, we'll be adding new ones! How? Basically, you'll create a new instance of this class: - then you'll need to do a little bit of magic to (A) scan for controller classes and (B) get a list of public methods via reflection and then use getMethodAnnotations to read them. What you're looking for is any Route annotations that you added. You can use these to manually create real Route objects (these: and add those to the collection. I realize that I just smashed 5 big steps into one here... but I thought I'd get you started... IF you're even interested.

    Honestly, this would make for a really good mini-screencast! Because it's totally possible! But, you need to understand a bit about how things work to make it happen. Anyways, if you actually want to make this happen and have some questions, let me know!


  • 2017-01-08 Matt Holbrook-Bull

    hi Ryan et all,

    is it possible to annotate routes a' la symfony? I hate routing files and much prefer everything to be together.. as we have a symfony core is there a way of achieving this without too much pain?



  • 2015-12-08 weaverryan

    Hey guys!

    Yes, you can of course create nodes in the admin interface like always. By "custom pages", I mean creating custom paths for custom interactions - not just for reading node content. For example, you could build an entire mini-application inside Drupal using routes that has *nothing* to do with the CMS - e.g. a set of pages where uses can send and receive messages or a set of pages containing a multi-step form for creating an "order" of some sort.


  • 2015-12-07 Jgrubb

    Pretty sure this was just intended to be a really simple demo module. You can still create pages as you always have in the UI.

  • 2015-12-07 WEB BY DOUG

    The concept that custom pages are modules is new to me. I've been developing drupal sites since 4.7 and never heard that before. Is that a new concept in D8?