Module development

Polavi application is open for customization. Developers can add/modify or remove some of the functions depends on the requirement of the project by developing a module.

Custom module and core module.

In Polavi app, there are 2 types of module

  • Core modules: They are located in <root>/app/src/Polavi folder.
  • Custom modules: They are located in <root>/extension folder.

You can check this article to understand about module structure and meaning of each element in Polavi module.

Folder name.

A module folder name must not have special characters and whitespace. We suggest following this format <Company><Module> For example "AbcCatalog"

Module version and description

You can define the module version and some extra information by declaring some variables in the "migration.php" file. This file located in the module folder.

Visioning

A version number can be defined by declaring a variable named "$version" in the migration.php file. The version number must follow this format: <number>.<number>.<number> For example: 1.0.1

The version number will be used to identify the module version and track for migration purposes.

Module information

You can define some extra information like description, author by declaring the following variables

  • $description: This is module description
  • $author: This is module author
  • $authorUrl: This is author URL

That information will be displayed in the module manager from the admin panel.

Installation and Migration

Installation

To install a module, follow the below steps:

  • Copy module folder to "extension" folder
  • Go to admin panel-> Extension manager and install it.

When you click install, Polavi will load migration.php file and execute the migration executor to create a database table. The version of the module is also remembered for the upgrade.

Migration

One of the important things in module development is migration. It happens when you need to modify database schema or adding some default value. In Polavi, you can do it by adding migration.php file to your module There are 2 important things in this file

  • Version
  • Migration executors

The version will be used to identify if there is a new version released or not Migration executors will be executed automatically if a new version is released.

Here is an example of a migration file

Version 1.0.0

<?php
$version = "1.0.1";
return [
"1.0.0" => function(\Polavi\Services\Db\Processor $conn) {
$conn->executeQuery("CREATE TABLE `your_module_table` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`status` smallint(6) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Comment'");
}
];

In the above file, we return an array, it is a list of migration executors. 1 array element is for 1 version. It has a key is actually module version number. Let's say today we release version 1.0.1 and need to add a new table. Now the migration.php will look like this

<?php
$version = "1.0.1";
return [
"1.0.0" => function(\Polavi\Services\Db\Processor $conn) {
$conn->executeQuery("CREATE TABLE `your_module_table` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`status` smallint(6) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Comment'");
},
"1.0.1" => function(\Polavi\Services\Db\Processor $conn) {
$conn->executeQuery("CREATE TABLE `your_second_module_table` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`status` smallint(6) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Comment'");
}
];

The application will track the version and do the comparison. If a higher version available, it will execute the migration functions automatically. In this case, a new table will be created.

If your module does not need to modify database, you can just simply return an empty array.

<?php
$version = "1.0.0";
return [];

Uninstall a module

You can go to the admin panel-> Extension manager to uninstall a module. For now, the uninstall process will not revert the changes that you made to the database schema.