Let’s create a demo module:
– create modules\demo folder
– create demo.info.yml inside that folder
– put minimally required content:
name: Drupal 8 Demo module
description: 'Demo module for Drupal 8 alpha11'
type: module
core: 8.x
– And that’s it, one file. You can now navigate to the Extend page, find the Demo module and enable it.
– create a routing file for our module called demo.routing.yml
– Inside this file, we can have the following (simple) route definition:
demo.demo:
path: '/demo'
defaults:
_controller: '\Drupal\demo\Controller\DemoController::demo'
_title: 'Demo'
requirements:
_permission: 'access content'
*
– The first line marks the beginning of a new route called demo for the module demo (the first is the module name and the second the route name).
– Under path, we specify the path we want this route to register.
– Under defaults, we have two things: the default page title (_title) and the _content which references a method on the DemoController class.
– Under requirements, we specify the permission the accessing user needs to have to be able to view the page.
– Now, let’s create our first controller called DemoController that will have a method named demo() getting called when a user requests this page.
– Inside the module directory, create a folder called src/ and one called Controller/ inside of it.
* This will be the place to store the controller classes.
– Go ahead and create the first one: DemoController.php
<?php
/**
* @file
* Contains \Drupal\demo\Controller\DemoController.
*/
namespace Drupal\demo\Controller;
/**
* DemoController.
*/
class DemoController {
/**
* Generates an example page.
*/
public function demo() {
return array(
'#markup' => t('Hello World!'),
);
}
}
* This is the simplest and minimum we need to do in order to get something to display on the page.
– All we have to do now is clear the caches and navigate to http://example.com/demo and we should see a Drupal page with Hello World printed on it.
* How to create a menu link that shows up under the Structure menu of the administration?
– create a file called demo.links.menu in the root of our module
demo.demo:
title: Demo Link
description: 'This is a demo link'
parent: system.admin_structure
route_name: demo.demo