8

How can I implement the most basic route in Drupal 8? What files do I have to write, and which classes do I need to implement?

11

Implementing the most basic route in Drupal 8 require:

  • The file containing the routing definitions (helloworld.routing.yml)

    helloworld.hi:
      pattern: '/helloworld'
      defaults:
        _controller: '\Drupal\helloworld\Controller\HelloWorldController::hi'
      requirements:
        _permission: 'access content'
    
  • A controller class that returns a render array or a string (src/Controller/HelloWordCOntroller.php)

    namespace Drupal\helloworld\Controller;
    
    class HelloWorldController {
      function hi() {
        return 'Hello world!';
      }
    }
    

This is the equivalent of what in Drupal 7 was a MENU_CALLBACK. Implementing a local task, or a local action requires other files to be added, differently from Drupal 7, where a local tab would be implemented using the same hook used for a MENU_CALLBACK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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