A module is a self-contained mini-application. It typically includes:
Modules promote clean separation of concerns and make large applications easier to maintain and extend.
Create a folder with the module name (use UpperCamelCase recommended) Example: News, Gallery, Shop
Inside the folder, create a file named Module.php
The file must follow PSR-4 autoloading and extend AbstractModule
Example
<?php
namespace News;
use Krystal\Application\Module\AbstractModule;
class Module extends AbstractModule
{
/**
* Returns routes defined by this module
*/
public function getRoutes()
{
return include __DIR__ . '/config/routes.php';
}
/**
* Returns service providers for this module
*/
public function getServiceProviders()
{
return [
'postManager' => new stdclass
];
}
/**
* Optional: module-specific configuration
*/
public function getConfigData()
{
return [
'version' => '1.2.3',
'author' => 'Your Name',
'description' => 'News & Blog module',
];
}
/**
* Optional: module translations per language
*/
public function getTranslations($lang)
{
return include __DIR__ . "/Translations/$lang.php";
}
}
These are inherited from AbstractModule and are mostly used inside controllers:
$this->getService($name) // Get a service registered in this module
$this->getServices() // Get all services of this module
$this->hasService($name) // Check if a service exists
$this->hasConfig($key = null) // Check if module config exists (or if a specific key exists)
| Method | Required? | Returns | Purpose |
|---|---|---|---|
getRoutes() |
Yes | array | Module routes (see Routing docs) |
getServiceProviders() |
Yes | array (name => factory / class) | Registers services for dependency injection |
getConfigData() |
Optional | array | Module metadata, settings, version info |
getTranslations($lang) |
Optional | array | Language-specific translation arrays |
public function indexAction()
{
$postManager = $this->getModuleService('postManager');
$posts = $postManager->getLatest(10);
}
public function someAction()
{
$userManager = $this->getService('Users', 'userManager');
$user = $userManager->findById(42);
}
$newsModule = $this->moduleManager->getModule('News');
$postManager = $newsModule->getService('postManager');
$currentModule = $this->moduleName; // e.g. 'News'
Inside any controller
<?php
// Current module service
$this->getModuleService('postManager');
// Specific module service
$this->getService('Shop', 'cartManager');
// Current module name
$this->moduleName;
// Full module instance (advanced)
$this->moduleManager->getModule('Gallery');
Modules are the foundation for building modular, maintainable applications . Keep your module folders clean, follow naming conventions, and leverage services for business logic.