The Module Manager keeps an ordered registry of module namespaces and resolves any
of them to a configuration array by convention — appending \Module to
the namespace, instantiating the class, and calling its getConfig()
method. Resolution is independent of registration: a module need not be added to
the manager before its config can be read.
By default, one module is provided with the JiNexus Framework, named
Application. It provides a controller to handle the "home" page of
the application, the layout template, templates for 404 and error pages, and its
own configuration via module.config.php.
Each module is a PHP class named Module directly under its namespace
(e.g. Application\Module), extending
AbstractModule and overriding getConfig(): array.
Let's create a new module named Blog. Below is the updated directory layout:
application_root/
config/
application.config.php
modules.config.php
data/
cache/
module/
Application/
Blog/
config/
module.config.php
src/
Controller/
Module.php
view/
blog/
public/
index.php
.htaccess
asset/
test/
Application/
Blog/
vendor/
First, create module/Blog/src/Module.php extending AbstractModule:
declare(strict_types=1);
namespace Blog;
use JiNexus\ModuleManager\ModuleManager\AbstractModule;
class Module extends AbstractModule
{
public function getConfig(): array
{
return [
'routes' => [
'blog' => [
'route' => '/blog',
'controller' => 'Blog\Controller\IndexController',
'action' => 'index',
],
],
];
}
}
Alternatively, use a separate config file and include it — the same pattern the
Application module follows:
public function getConfig(): array
{
return include __DIR__ . '/../config/module.config.php';
}
Next, map the Blog namespace using PSR-4 autoloading in
composer.json:
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
"Blog\\": "module/Blog/src/"
}
},
"autoload-dev": {
"psr-4": {
"Application\\Test\\": "test/Application/",
"Blog\\Test\\": "test/Blog/"
}
}
Then dump the autoloader so Composer picks up the new namespace:
composer dump-autoload
Lastly, enable the new module by adding it to config/modules.config.php:
declare(strict_types=1);
/**
* List of enabled modules for this application.
*
* This should be an array of module namespaces used in the application.
*/
return [
'Application',
'Blog',
];
In modules.config.php all modules are loaded from top
to bottom. Each module configuration is merged via
array_merge_recursive into one combined configuration that
includes routes, view_manager, and other settings. If the same
keys exist across modules, the later module's values are merged on top of
the earlier ones.
If you found a typo or error, please help us improve this document. Create Issue