Adding a basic controller
This example uses ControllerBase which can greatly reduce boilerplate dependency handling code. However, it also makes the class considerably more difficult to unit test. Therefore this base class should only be used by controller classes that contain only trivial glue code. Controllers that contain sufficiently complex logic that's worth testing should not use this base class but should instead use ContainerInjectionInterface or should even better be refactored to be trivial glue code.
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
/src/Controller/HelloController.php
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, on its own, will not do anything. It needs to be invoked by adding a routing file to our module (see previous step). Adding the controller first to our code, however, is part of a general D8 philosophy of, "Build a tool, then wire it up".
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion