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
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
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.
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
The end point
Then create landing page for your module my-module/index.php
1<?php
2
3namespace MyCompany\iTop\SystemInformation;
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.
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: