Fork me on GitHub

Documentation

Module Manager

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.

Module

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.

Create a new Module

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',
    ];