Skip to main content

Overview

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.

 

Parameters in Route :

Drupal 8's routes may include placeholder elements which designate places where the URL contains dynamic values. In the controller method, this value is available when a variable with the same name is used in the controller method. For example in example.routing.yml:

 

example.name:
  path: '/example/{name}'
  defaults:
    _controller: '\Drupal\example\Controller\ExampleController::content'
  requirements:
    _permission: 'access content'

 

If you're only modifying or extending existing functionality, you may not need to know about routes. However, if you want to expose content or functionality on your own URIs on a site, routing is an important part of writing your module. This can help provide functionality at specific URIs of a website or just modify or augment existing functionality.

 

Routing in routing.yml File :

 

example.content:
  path: '/example'
  defaults:
    _controller: '\Drupal\example\Controller\ExampleController::content'
    _title: 'Hello World'
  requirements:
    _permission: 'access content'

 

This tells Drupal 8 that a route named 'example.content' (named with the module name as prefix) exists and is bound to the URI '/example' on the site. When that path is accessed, the 'access content' permission is checked on the accessing user and, if access is granted, the ExampleController::content method is invoked and a page is displayed with the title 'Hello World'.

 

Structure of Routes :

The simplest way to define routes is to create a my_module_name.routing.yml file

  • path (required): The URL to the route, with a leading forward slash (e.g., path: '/book'). You can use dynamic properties by including them in curly braces. (e.g., path: '/node/{node}/outline'). These will be passed along as arguments via parameter converters to the controller/form (see below). Note that the first item of the path cannot be an argument, and must be a string. You can also define optional parameters at the end of your path

 

  • defaults (required): Defines the default properties of a route. Provide one of the following to specify how the output is generated:
    • _controller: A Callable. Allows you to map to a callable function in one of the following ways:
      • Class::method:\Drupal\[my_module_name]\Controller\[ClassName]::[method] example: _controller: '\Drupal\acme\Controller\AcmeController::build' This will execute the build() method (function) that is in the class AcmeController, that exists in the namespace Drupal\acme\Controller

        Note that this value is not a path on the file system, but rather a PSR-4 namespace. Also note that module names are lower-case with underscores, while class names are camel-case.

      • Service: controller.test:[method], where "controller.test" is a Drupal 8 service, defined in a *.services.yml file. example: _controller: 'acme.controller:build'

        This will execute the build() method (function) that is in the class defined in acme.services.yml for the acme.controller service.

Published on