Skip to main content
The upcoming Drupal 8 release provides more than 200 new features and improvements from over 2400 contributors. Drupal 8 development is expected to set new benchmarks for ease of use, responsiveness and faster page loads for high performance. In the meantime, Drupal 8 is currently in the beta stage of the release cycle. We’ll have a look about the routing system in Drupal 7 and then learn how the routing process of Symphony is adopted in Drupal 8.
 
In Drupal, a request is handled on the basis of the path it is recognized and returns to some sort of content. Else, Drupal gives a 404. Drupal maps the URL paths with the relevant page and access callbacks.
 
Drupal 7
 
In Drupal, hook allows you to register paths to access titles, arguments and other requirements.  Drupal 7 modules make use of associated page callbacks to paths in hook_menu().
 
Hook_menu()
Menu links
Breadcrumbs
Contextual links
Title
 
function example_menu() {
  $items['hello'] = array(
    'title' => 'Hello world',
    'page callback' => '_example_page',
    'access callback' => 'user_access',
    'access arguments' => 'access content',
    'type' => MENU_CALLBACK,
  );
  return $items;
}
 
Drupal 8
 
Drupal 8 architecture will feature the routing process of Symfony, which offers much more flexibility and simplicity. Drupal 8 is taking the route of Symfony, which makes it easier for Drupal developers to get started. 
 
In Drupal 8 page callbacks are transformed into responses. The available routes are now configured in a file called{module}. routing.yml?in the module folder. A path will return multiple routes.
 
example.routing.yml
example.hello:
  path: '/hello'
  defaults:
    _content: '\Drupal\example\Controller\Hello::content'
  requirements:
    _permission: 'access content'
'Defaults' keys:
Defines the default properties of a route. Provide one of the following to specify how the output is generated.
_controller
    Drupal 8 provides a method to generate a response.
_content
    If specified, the _controller is set based on the request's mime type, and fills the content of the response with the result of the specified method (usually a string or render array).
_form
    If specified, the _controller is set to HtmlFormController::content, which responds with the specified form. This form must be a fully qualified class name (or service id) that implements FormInterface and usually extends FormBase. Indeed, form building has also become object oriented!
_entity_form
    If specified, the _controller is set to HtmlEntityFormController::content, which responds with the specified entity form (specified as {entity_type}.{add|edit|delete}).
_entity_list
    If specified, the _controller is set to HtmlFormController::content, and _content to EntityListController::listing, which renders a list of entities based on the entity type's list controller.
_entity_view
    If specified, the _controller is set to HtmlFormController::content, and _content to EntityViewController::view, which renders the entity based on the entity type's view controller.
_title
    The title of the page (string).
_title_callback
    The title of the page (method callback).
 
'Requirements' keys:
Determines what conditions must be met in order to grant access to the route.
 
_permission
    The current user must have the specified permission.
_role
    The current user must have the specified role.
_method
    The allowed HTTP methods (GET, POST, etc).
_scheme
    Set to https or http. The request scheme must be the same as the specified scheme. This property is also taken into account when generating URLs (Drupal::url(..)) rather than routing. If set, urls will have this scheme set fixed.
_node_add_access
    A custom access check for adding new nodes of some node type.
_entity_access
    A generic access checker for entities.
_format
    Mime types.
 
Subscribe to Drupal Routing
 

start with anubavam today

You have an idea we have engineers to convert your ideas into reality

Request Quote