As in Drupal 7, I had this in mymodule_menu() (Implementing hook_menu()):

$items['admin/config/mymodule/page1'] = array(
    'title'             => 'Mymodule Page1',
    'page callback'     => 'mymodule_page1',
    'file'              => 'mymodule.admin.inc',

So, basically in Drupal 7, whenever I navigated to /admin/config/mymodule/page1, it showed me the output of mymodule_page1 function stored in mymodule.admin.inc file.

Modifying this to Drupal 8, I did:

  $items['mymodule.page1'] = array(
    'title'              => 'Mymodule Page1',
    'route_name'         => 'mymodule.page1',

My routing.yml file:

  path: '/admin/config/mymodule/page1'

What should be the corresponding code in defaults and requirements such that the function mymodule_page1 gets outputted on navigating to that route?

  • Routing in Drupal 8 is adopted from symfony2. While this is not going to answer your question but a quick look at symfony.com/doc/current/book/routing.html may help.
    – awm
    Commented Jun 11, 2014 at 14:42
  • @awm: Thanks. I know about this. I just needed to clear my doubt concerning Symfony's implementation in Drupal 8.
    – xan
    Commented Jun 11, 2014 at 15:29
  • The Drupal 8 routing API handbook is at drupal.org/developing/api/8/routing
    – user49
    Commented Jun 11, 2014 at 16:20

2 Answers 2


You can't route to a function, you need to move the content of your function to a controller class and then specify _content: Drupal\yourmodule\Controller\SomeController::someMethod in _defaults, and a _permission: 'some permission' or other access check in requirements.

hook_menu() no longer exists, instead, move your default menu links to a yourmodule.menu_links.yml file.

Almost every core module has plenty examples for this, the necessary documentation has also been linked in the comments.


Try this:


 * @file
 * Contains \Drupal\mymodule\EventSubscriber\ContribRoutesOverride.

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

 * Class MymoduleRoutesOverride
class MymoduleRoutesOverride implements EventSubscriberInterface {

   * {@inheritdoc}
  public static function getSubscribedEvents() {
    $events[RoutingEvents::ALTER] = 'alterRoutes';
    return $events;

   * Alters existing routes.
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The route building event.
  public function alterRoutes(RouteBuildEvent $event) {
    // Fetch the collection which can be altered.
    $collection = $event->getRouteCollection();
    // Get routes
    $routes = $collection->getIterator();

    foreach ($routes AS $route_name => $route) {
        if ($route_name == 'this_are_the_droids_youre_looking_for') {
          $route->setDefault('_content', '\Drupal\mymodule\Myresponder::page');


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.