Modules
Modules in Quanta are used to encapsulate specific functionality, often related to a particular feature or service within the application. Modules can interact with various components, including routes, database, and actions. They provide a structure for organizing code and improving maintainability.
The Module
Class Structure
In Quanta, the Module class serves as a base class for all modules. Every module must inherit from this class and implement its abstract methods load() and dispose().
namespace Quanta\Core;
use Quanta\Quanta;
/**
* The base class for the modules
*/
abstract class Module {
protected $id;
public function __construct(string $id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
/**
* Initializes the module. This method must be implemented by subclasses.
*
* @param Quanta $quanta The instance of the Quanta framework.
*/
abstract public function load(Quanta $quanta);
/**
* Disposes of the module. This method must be implemented by subclasses.
*
* @param Quanta $quanta The instance of the Quanta framework.
*/
abstract public function dispose(Quanta $quanta);
}
Description of the Methods:
-
load(Quanta $quanta)
: This method is called when the module is loaded. It is where the module is initialized. You can add custom actions, set up database tables, or configure other settings here. -
dispose(Quanta $quanta)
: This method is called when the module is no longer needed or when it is being removed from the Quanta application. It is used for cleanup tasks, such as closing connections or freeing resources. -
getId()
: This method returns the ID of the module. The ID is set in the constructor and can be used to uniquely identify the module.
Example of a Custom Module: UserModule
If you wanted to create a user management module, you would define it as follows. In this case, the load method could register actions for user creation, while the dispose method could perform cleanup tasks.
namespace App\Modules;
use Quanta\Core\Module;
use Quanta\Quanta;
class UserModule extends Module {
public function load(Quanta $quanta)
{
// Initialization of the module: e.g., registering actions
echo "UserModule is being loaded.\n";
}
public function dispose(Quanta $quanta)
{
// Cleanup of the module: e.g., closing connections or clearing temporary data
echo "UserModule is being disposed.\n";
}
}
Explanation:
-
load():
This method could be used for various initialization tasks, like setting up database tables for users or registering custom actions, such as CreateUserAction or AuthenticateUserAction. Furthermore, you can initialize components, routes, and all other necessary elements required to work with your module. -
dispose():
This method can be used for cleanup tasks, such as closing database connections or deleting temporary files created during the module's use.
Example of Using a Module in Quanta
To use the UserModule
in a Quanta project, you would instantiate and load the module like this:
Instantiating and Loading the Module:
$userModule = new UserModule("usermodule");
$quanta->addModule($userModule);
$quanta->loadModules();
You also can access an loaded module with the following function
$userModule = $quanta->moduleHandler->getModuleWithID("usermodule");