Drupal 8 Create a custom page

For creating custom page in Drupal 8, there are two Steps.

Step 1:  First, you need to declare the routing in <module_name>.routing.yml.

Step 2: Secondly, you need to add a controller that returns the page body in    example/src/Controller/ExampleController.php.

Example are below for creating a simple page through custom module.

#Declaring a routes

The routing information is stored in example/example.routing.yml:






example.my_page

This is the machine name of the route. By convention, route machine names should be module_name.sub_name. When other parts of the code need to refer to the route, they will use the machine name.

path

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

defaults

This describes the page and title callbacks. @todo: Where can these defaults be overridden? 

requirements

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

#Add a controller (page callback)

The controller returns the page body. It must be either a class method or a registered service. It can be different depending on various conditions (HTTP vs. HTTPS, content headers, others) but that is beyond the scope of this introduction.

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

namespace

This declares the prefix needed to fully qualify the name of the class we are defining. Compare the file's doc block and the name of the class. The class auto-loader knows that, to find the class \Drupal\example\Controller\ExampleController, it should look for the file modules/example/src/Controller/ExampleController.php.

use

This allows us to use ControllerBase instead of the fully qualified name. This makes our class line much easier to read.

 myPage()

The method specified in the YAML file must be public. It should return a renderable array.



Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...