The Controller

Now that we have found out more or less where we have to place our Controller, let's begin by creating a Controller folder inside our module's /src folder. Although not mandatory, this is a standard practice for Controller placement. Inside this folder, we can have our first Controller class file, that is, HelloWorldController.php.

Inside the file, we again have something simple (after the opening PHP tags):

namespace Drupal\hello_world\Controller;use Drupal\Core\Controller\ControllerBase;/** * Controller for the salutation message. */class HelloWorldController extends ControllerBase {  /**   * Hello World.   *   * @return string   */  public function helloWorld() {    return [      '#markup' => $this->t('Hello World')    ];  }}

As expected, we ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.