Adding main controller
The content() function in the HelloController class will return markup text when the routing system calls the page.
In your module folder, you should have the standard PSR-4 folder structure /src/Controller, and inside this folder, you should have the controller file HelloController.php.
Thus, your controller file will be located at
/src/Controller/HelloController.php
You have the following code in the HelloController.php file:
<?php namespace Drupal\hello_world\Controller; use Drupal\Core\Controller\ControllerBase; /** * Defines HelloController class. */ class HelloController extends ControllerBase { /** * Display the markup. * * @return array * Return markup array. */ public function content() { return [ '#type' => 'markup', '#markup' => $this->t('Hello, World!'), ]; } }
This code alone will not do anything. It must be triggered by adding a routing file to our module. However, adding the controller to our code first is part of the general D8 philosophy: “Build the tool, then connect it.”
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.