Adding a basic controller

Last updated on
3 March 2017

The content() function in the HelloController class will be returning the markup text, when the routing system invokes the page.

Within your module folder, you should have the PSR-4 standard folder structure /src/Controller and inside this folder you should have your HelloController.php controller file.
So your controller file will be like this

Have the following code in the HelloController.php file:


namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloController extends ControllerBase {

   * Display the markup.
   * @return array
  public function content() {
    return array(
      '#type' => 'markup',
      '#markup' => $this->t('Hello, World!'),


This code, on its own, will not do anything. It needs to be invoked by adding a routing file to our module. Adding the controller first to our code, however, is part of a general D8 philosophy of, "Build a tool, then wire it up".