2. Hello World!

The controller

Create a controller into my-module/src/Controller, let’s call it MyModuleController extending Combodo\iTop\Application\TwigBase\Controller\Controller

src/Controller/MyModuleController.php
1<?php
2
3namespace MyCompany\iTop\MyModule\Controller;
4use Combodo\iTop\Application\TwigBase\Controller\Controller;
5
6class MyModuleController extends Controller
7{
8}

Let’s add a Hello World operation

src/Controller/MyModuleController.php
1<?php
2
3class MyModuleController extends Controller
4{
5    public function OperationHelloWorld()
6    {
7        $this->DisplayPage();
8    }
9}

This will just display the Twig template corresponding to this operation. Here the operation is HelloWorld without space. The name of the method is built from the operation name by adding Operation at the beginning: OperationHelloWorld.

Calling the method DisplayPage() will render the template named after the operation: HelloWorld.html.twig it will be located in the folder my-module/templates.

The template

Let’s create the template my-module/templates/HelloWorld.html.twig with a nice title.

templates/HelloWorld.html.twig
1{% UITitle ForPage {sTitle:'Hello World!'} %}{% EndUITitle %}

Twig syntax can be found Here.

For more information on specific iTop Twig tags you can check UI Components and UI Layouts, note that UI blocks are new in iTop 3.0 and cannot be used in iTop 2.7

The end point

Then create landing page for your module my-module/index.php

index.php
 1<?php
 2
 3namespace MyCompany\iTop\MyModule;
 4
 5use MyCompany\iTop\MyModule\Controller\MyModuleController;
 6
 7require_once(APPROOT.'application/startup.inc.php');
 8
 9$oUpdateController = new MyModuleController(MODULESROOT.'my-module/templates', 'my-module');
10$oUpdateController->SetDefaultOperation('HelloWorld');
11$oUpdateController->HandleOperation();

Create an instance of the controller indicating the templates path and the module name. The default operation is set to the operation we want when entering the module. The method HandleOperation() will call the method corresponding to the specified operation.

Now you have to build the autoloader by running composer dump-autoload into the module folder my-module.

The next operation is the setup. You will be able to select your module.

../../_images/Setup.png

For more comfort during the development of your module, you can install the toolkit and update your iTop with symlinks.

if you go to your module page https://localhost/itop/pages/exec.php?exec_module=my-module&exec_page=index.php you should see:

../../_images/Step2.png

You will notice that if you work with an iTop from an official package (not the sources from https://github.com/Combodo/iTop, when you modify the twig template, the result does not change.

In this case, you’ll have to add the following parameter to your iTop configuration:

<?php

/**
 *
 * Configuration file, generated by the iTop configuration wizard
 *
 * The file is used in MetaModel::LoadConfig() which does all the necessary initialization job
 *
 */
$MySettings = array(
    // developer_mode.enabled: If true then unlocks dev env functionalities, see \utils::IsDevelopmentEnvironment
    'developer_mode.enabled' => true,

    // ...
);