Skip to content

Instantly share code, notes, and snippets.

@rakeshjames
Created September 30, 2014 12:04
Show Gist options
  • Save rakeshjames/e2588a1d384b5d33f89a to your computer and use it in GitHub Desktop.
Save rakeshjames/e2588a1d384b5d33f89a to your computer and use it in GitHub Desktop.
Drupal 8 Custom module example
name: Drupal 8 custom module example
type: module
description: 'Example for Drupal 8 modules.'
package: Custom
version: 8.x
core: 8.x
<?php
/**
* @File
* Example custom module for Drupal 8.
* @author Rakesh James
*/
/**
* Implementing hook_menu().
*/
function example_menu() {
// The paths given here need to match the ones in example.routing.yml exactly.
$items['/mypage/page'] = array(
'title' => 'First page',
'description' => 'This is a example page.',
// The name of the route from example.routing.yml
'route' => 'example.my_page',
);
return $items;
}
example.my_page:
path: '/mypage/page'
defaults:
_content: '\Drupal\example\Controller\ExampleController::myPage'
_title: 'My first page in Drupal8'
requirements:
_permission: 'access content'
<?php
/**
* @file
* @author Rakesh James
* Contains \Drupal\example\Controller\ExampleController.
* Please place this file under your example(module_root_folder)/src/Controller/
*/
namespace Drupal\example\Controller;
/**
* Provides route responses for the Example module.
*/
class ExampleController {
/**
* Returns a simple page.
*
* @return array
* A simple renderable array.
*/
public function myPage() {
$element = array(
'#markup' => 'Hello world!',
);
return $element;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment