Skip to main content

In Drupal 8, We need to keep our custom,  contributed module in root directory.

modules/custom

modules/contrib

 

Create a custom module

Add custom_page.info.yml file to store metadata about the module.

modules/custom/custom_page/custom_page.info.yml

name: Custom page
description: 'Custom Page' 
type: module 
core: 8.x 
package: Custom

 

Declare the path and its options

In drupal7, there is a hook_menu() to declare the path.

In drupal8, create the file custom_page.routing.yml.

 

Codes to return the body content of the page

Routing Yaml File:

The routing information is stored in custom_page.routing.yml.

custom_page.my_page:
  path: '/mypage/page'
  defaults:
    _controller: '\Drupal\custom_page\Controller\ExampleController::myPage'
    _title: 'My custom page in Drupal8'
  requirements:
    _permission: 'access content'
​

custom_page.my_page - This is the machine name of the route.

path - This gives the path to the page on your site. Note the leading slash (/).

defaults - This describes the page and title callbacks.

requirements - This specifies the conditions under which the page will be displayed. You can specify permissions, modules that must be enabled, and other conditions.

 

Controller php File:

The Controller class ExampleController should be defined in custom_page/src/Controller/ExampleController.php:

namespace Drupal\custom_page\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Provides route responses for the Example module.
 */
class ExampleController extends ControllerBase {

  /**
   * Returns a simple page.
   *
   * @return array
   *   A simple renderable array.
   */
  public function myPage() {
    $element = array(
      '#markup' => 'Hello, world',
    );
    return $element;
  }

}

 

After clear the cache and see the following output:

 

custom page

Contributor(s)

Comments

Today, SRSG has emerged as one of the leading full-service player offering an array of products and services for system integration, broadcast consultancy, IT infrastructure services, maintenance services and digital archiving services for the broadcast industry.
Apple service center Delhi
Apple iphone 7 reseller in Kolkata
Apple iphone 6S reseller in delhi
Mac pro service center in Guwahati Apple ipad air mini 2 Service center in Delhi
Apple ipad air dealers in Kolkata
iMac dealers in Delhi
ipad dealers in Mumbai