This section covers the module development and guides you through a simple example to get you started.

Let's create a simple example module, this will have only very rudimentary functionality, but it should be enough to explain the process of module developing for Scriptor CMS.

The most important thing is your module class that extends Scriptor's Module core class:

<?php 
namespace Scriptor;

class TestModule extends Module
{
    // empty module
}

Save the class you are creating in a file with the same name as the class in the /site/modules/TestModule/ directory. For example, a module with the TestModule class (as above) would be in a file named TestModule.php.

We could now use with our TestModule module all the methods and functions it inherited from the parent (Module) class. However, the module should first be initialized by calling the parent class' init() method:

<?php 
namespace Scriptor;

class TestModule extends Module
{
    /**
     * Method to perform any initialization.
     */
    public function init() 
    {
        // This loads the most important variables of the parent class
        parent::init();
    }
}

Our module does not do much yet, let's add a hello() method to it that returns the string Hello name, where name would be the current user's name:

<?php 
namespace Scriptor;

class TestModule extends Module
{
    /**
     * Method to perform any initialization.
     */
    public function init() 
    {
        // This loads the most important variables of the parent class
        parent::init();
    }

    public function hello($name) 
    {
        return 'Hello '.$this->sanitizer->text($name);
    }
}

Tell Scriptor about your module

Since you just creating the file, Scriptor might not be able to see your module yet, so your newly created module must still be made accessible to Scriptor. Take a quick look at the example of a module registration that should be done in the custom.scriptor-config.php file:

<?php defined('IS_IM') or die('You cannot access this page directly');

$config = array_replace_recursive($config, [
    'modules' => [
        'TestModule' => [
            'menu' => '',
            'position' => 0,
            'active' => true,
            'auth' => false,
            'autoinit' => true,
            'path' =>  'modules/TestModule/TestModule',
            'class' => 'TestModule',
            'display_type' => [
            ],
            'description' => "Scriptor's test module."
        ]
    ]
]);

For more information about the individual parameters, read here.

Loading the module

After the TestModule has been made accessible to the Scriptor, you can call upon this module from any of your site template files and use it we can use it in our template by loading:

$testModule = $site->loadModule('TestModule');

To avoid ugly warnings, you should always check whether the specified module has been loaded:

$testModule = $site->loadModule('TestModule');
// Check if the module is loaded
if($testModule) {
    ...
}

Note that when the module is loaded, its init() method is automatically called. However, if you want to avoid automatic initialization, you can achieve this by setting the option parameter autoinit of the loadModule() method to false:

$testModule = $site->loadModule('TestModule', ['autoinit' => false]);
...

Using the module

The module is ready for use, the method hello() can be called as follows:

$testModule = $site->loadModule('TestModule');
if($testModule) {
    echo '<h3>' . $testModule->hello('Lucas') . '</h3>';
}

The output would be <h3>Hello Lucas</h3> or whatever name you passed to the method.

This represents our basic introduction to the development of a simple module.