5.1. Modules

One of the advantages of a modular architecture, where the modules are self contained, is that you can replace or add any module without affecting the rest of the system.

In Trex, each module is an object instance and can be accessed from anywhere within a project. Module are stored in folders that are named in a way that can be associated with a URI segment.

The adding of modules is accomplished in a configuration file (config/ModuleConfig.php) or, if the Admin Site is enabled, using the “Module Management” panel. When a module is added, the module’s capabilities must also be added. These capabilities are always checked before allowing the Controller_Router to make a request of a module. Also, other attributes may be specified for a module, e.g. the “hidden” attribute is currently used to determine whether the module should appear in the main navigation.

5.1.1. Module Architecture

In Trex, modules are self-contained components, this means that you can replace or add any module without affecting the rest of the system. Projects can contain one or more modules. Each module has its own MVC implementation and its own subdirectory in the modules/ directory.

Modules can be public or protected. Protected modules cannot be added using the Admin Site.

In addition, you can have a flat or vertical module structure. In vertical structures you can have parent/child modules (module A inherits from module B), and modules within modules.

This is the typical tree structure of a module:

news/
    config/
        NewsConfig.php
    models/
        NewsModel.php
        NewsAdminModel.php
    public/
        css/
        js/
    views/
        frontend/
            index.html
        backend/
            index.html
    News.php
    NewsAdmin.php

The following files and directories are required to create a module:

news/
    views/
        frontend/
            index.html
    News.php

5.1.2. Directory Description

config/ Can contain custom configuration files with local parameters for the module.
includes/ Stores classes specific to the module.This classes can also extend the core classes.
models/ Stores the object model of the module. Models are shared across the system.
public/ Can contain files accessible from the Internet specific to the module.
css/ Contains style sheets with a .css extension.
images/ Contains images with a .jpg, .png, or .gif format.
js/ Holds JavaScript files with a .js extension.
views/ Contains the templates corresponding to the actions of the module.
frontend/ Front-end templates.
backend/ Back-end templates (admin).

5.1.3. Action Controllers Overview

Front-end Action Controller example:

http://www.domain.com/news/

In the above example, the system would attempt to find a folder named “news” and an Action Controller named News.php. If the name matches the first segment of the URI, the Action Controller will be loaded. If a URI contains more than two segments they will be passed to a module as parameters.

Back-end Action Controller example:

http://www.domain.com/admin/news/

If we add the “admin” keyword to the URL path, the system would attempt to find a folder named “news” and an Action Controller named NewsAdmin.php.

The back-end Action Controller is optional. If your project doesn’t have a back-end application, you don’t need to add one.

trex-module.png
Figure 5.1.3 - Action Controllers

5.1.4. Configuration

By default, a module inherits the default configuration from the global configuration file (ModuleConfig.php) located in the config/ directory. Modules can override global settings by placing a configuration file in the <module_name>/config/ directory. For instance, you might need to do this to change the main template or force a user to authenticate.

The module configuration file should always be named <module_name>Config.php, using the CamelCase convention.

Example: Inheritance

config/
  ModuleConfig.php
app/
  modules/
    news/
      config/
        NewsConfig.php

Example: Global Configuration file

config/ModuleConfig.php

class ModuleConfig
{
        public function getConfigArray()
        {
                return array(
                        "debug" => true,
                        "authentication" => false,
                        "https" => false,
                        "leftmenu" => false,
                        "templates" => array("main"=>"Main.html", "frame"=>"Frame.html"),
                );
        }
}

Example: News Module Configuration file

app/modules/news/config/NewsConfig.php

class NewsConfig
{
        public function getConfigArray()
        {
                return array(
                        "authentication" => true,
                        "templates" => array("main"=>"Main.html", "frame"=>"NewsFrame.html"),
                );
        }
}

In the example above, the News module inherits the configuration from the global configuration file and overwrites the “authentication” and “templates” settings.

Pages: 1 2