Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Wim Leers on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
Description:
With the introduction of the Drupal 8 routing system, hook_menu()
page callbacks throughout Drupal core have been replaced with new controller classes. (See also: ControllerBase class available for routing controllers to minimize boilerplate code.)
Drupal 7
hello.module
function hello_menu() {
return array(
'hello' => array(
'title' => 'Hello world',
'page callback' => 'hello_page',
'access callback' => 'user_access',
'access arguments' => array('access content'),
),
);
}
function hello_page() {
return array(
'#type' => 'markup',
'#markup' => t('Hello.'),
);
}
Drupal 8
hello.routing.yml
hello:
path: '/hello'
defaults:
_controller: '\Drupal\hello\Controller\HelloController::content'
_title: 'Hello world'
requirements:
_permission: 'access content'
src/Controller/HelloController.php
namespace Drupal\hello\Controller;
class HelloController {
public function content() {
return array(
'#type' => 'markup',
'#markup' => t('Hello.'),
);
}
}
Impacts:
Module developers
Comments
Pass page arguments to controller
In D6/7 there was a possibility to pass `page arguments` to `page callback`:
Is there smth similar in Drupal 8?