Modules

Zemit is a Multi-Module Phalcon Application using the MVC (model–view–controller) pattern. You can create your own module and extend the existing modules configured in Zemit Core. The module are configured within the default Configuration component injected into the DI of the bootstrap.

Zemit Bootstrap will read the module section of the configuration and then register the proper paths of the defined Module.php file as well as the default route.

Module Configuration

Zemit Core comes with some default modules. You can override or unset any of these of necessary.

Creating your own Module

Creating your own module is pretty simple. Let's say you want to create your own module named Custom.

This suggested structure below is the standard used by Zemit Core. By using this standardized structure, you won't have to override getViewsDir() and getNamespaces() methods of the Zemit\Mvc\Module class to fit your needs.

app/Modules/Custom/...
     - Controllers/...
     - Models/...
     - Views/...
     - Model.php
     - Controller.php
     - Modules.php

Creating the new custom module class

You will have to create a new Module file ./app/Modules/Custom/Modules.php which will extend from the \Zemit\Mvc\Module class.

// ./app/Modules/Custom/Modules.php
namespace App\Modules\Custom;

class Module extends \Zemit\Mvc\Module
{
    // put your module name here
    public $name = 'custom';
}

Add the custom module into the Configuration

// ./app/Bootstrap/Config.php
namespace App\Bootstrap;

class Config extends \Zemit\Bootstrap\Config
{
    public function __construct($config = [])
    {
        parent::__construct([
            // ...
            'modules' => [
                // New custom module, the key is the module name
                'custom' => [
                    'className' => APP_NAMESPACE . '\\Modules\\Custom\\Module',
                    'path' => APP_PATH . '/Modules/Custom/Module.php',
                ],
            ],
            // ...
        ]);
        if (!empty($config)) {
            $this->merge(new \Phalcon\Config($config));
        }
    }
}

Removing a default Zemit Module

Since Zemit Core define some modules by default, if you would like to remove these, you will have to remove them from the Configuration before they are registered. In order to achieve this, here is an example by overriding the Config and unsetting the modules variables you'd like to remove.

// ./app/Bootstrap/Config.php
namespace App\Bootstrap;

class Config extends \Zemit\Bootstrap\Config
{
    public function __construct($config = [])
    {
        parent::__construct([
            // ...
        ]);
        
        // Remove the module you want by unsetting the variable
        unset($this->modules->frontend);
        unset($this->modules->admin);
        unset($this->modules->api);
        unset($this->modules->cli);
        unset($this->modules->oauth2);
        
        if (!empty($config)) {
            $this->merge(new \Phalcon\Config($config));
        }
    }
}

Overriding a default Zemit Module

// ./app/Bootstrap/Config.php
namespace App\Bootstrap;

class Config extends \Zemit\Bootstrap\Config
{
    public function __construct($config = [])
    {
        parent::__construct([
            
            // Overriding modules
            'modules' => [
                
                // Overriding Console Module
                'cli' => [
                    'className' => APP_NAMESPACE . '\\Modules\\Cli\\Module',
                    'path' => APP_PATH . '/Modules/Cli/Module.php',
                ],
                
                // Overriding API Module
                'api' => [
                    'className' => APP_NAMESPACE . '\\Modules\\Api\\Module',
                    'path' => APP_PATH . '/Modules/Api/Module.php',
                ],
                
                // Overriding Admin Module
                'admin' => [
                    'className' => APP_NAMESPACE . '\\Modules\\Admin\\Module',
                    'path' => APP_PATH . '/Modules/Admin/Module.php',
                ],
                
                // Overriding Frontend Module
                'frontend' => [
                    'className' => APP_NAMESPACE . '\\Modules\\Frontend\\Module',
                    'path' => APP_PATH . '/Modules/Frontend/Module.php',
                ],
                
                // Overriding oAuth2 Module
                'oauth2' => [
                    'className' => APP_NAMESPACE . '\\Modules\\OAuth2\\Module',
                    'path' => APP_PATH . '/Modules/OAuth2/Module.php',
                ],
            ],
        ]);
        
        if (!empty($config)) {
            $this->merge(new \Phalcon\Config($config));
        }
    }
}

Last updated