Drupal 8 routing

 A route is a path which is defined for Drupal to return some sort of content on. For example, the default front page, '/node' is a route. When Drupal receives a request, it tries to match the requested path to a route it knows about. If the route is found, then the route's definition is used to return content. Otherwise, Drupal returns a 404.

Routing system in Drupal 8 replaces hook_menu() in Drupal 7. The parts of hook_menu() that we are used for  creating menu entries, tabs, actions and contextual links are taken over by other subsystems. 

Drupal's routing system works with the Symfony HTTP Kernel. To learn the basics of Drupal 8's routing system, the Symfony Routing component's documentation is an excellent place to start. 

Routes -

A route determines which code should be run to generate the response when a URI is requested. It does this by mapping a URI to a controller class and method. This defines how Drupal deals with a specific URI.

Routes are stored in a YAML file (check out last week's tutorials on YAML files) in the root of the module and use the following naming convention.

modulename.routing.yml

Example - 

hello.content:

  path: '/hello'

  defaults:

    _controller: 'Drupal\hello\Controller\HelloController::content'

    _title: 'Hello world'

  requirements:

    _permission: 'access content'

The name of the route is hello.content. The path is /hello, which is the registered URL path. So when a user enters sitename.com/hello, this route will decide which code should be run to generate a response.

We then have two default configurations specified, the controller and the title.

The controller tells Drupal which method to call when someone goes to the URL for the page (which is defined in the route).

look at the path included in _controller.

'Drupal\hello\Controller\FirstController::content’

This comprises of a namespaces class, a double colon and then the method to call.

hello.content:

  path'/hello'

  defaults:

    _controller'Drupal\hello\Controller\HelloController::content'

    _title'Hello world'

  requirements:

    _permission'access content'

The title is the default page title. So when a user goes to /hello, the page title will be ‘Hello world’

Controllers - .

Controllers take requests or information from the user and decide how to handle the request. For this module, the controller is responsible generating the content (the ‘Hello world’ message) and returning it for the page.

A controller that returns a simple Hello world message looks like this.

<?php

/**

 * @file

 * Contains \Drupal\hello\Controller\HelloController.

 */

namespace Drupal\hello\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloController extends ControllerBase {

  public function content() {

    return array(

      '#type' => 'markup',

      '#markup' => t('Hello world'),

    );

  }

}

 The controller is returning a renderable array with “Hello world”. So if you hit /hello once all this is in place, you will get a Drupal page with a Hello world message.

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)   {   $...