Module design, part 3: The Index

Module design, part 3: The Index

August 3, 2013 - by Patrick van Bergen

Tags: ,

library index.jpg

In this part I will explain how ProBase hands a page-request over to the right function.

ProBase, Procurios' application framework, uses the Front Page Controller pattern. That means that all page requests are handled by a single point of entry, the Core.

When the Core has established the module for which the request is intended, it sends the request to the ModuleIndex of that module. This index selects the controller class that handles the request.

The ModuleIndex uses a simple array map to map url paths to controller functions. Here is an example of such a map:

class RelationIndex extends ModuleIndex
{
    static function root()
    {
        return array(
            ''                                        => array('RelationController', 'relationOverview'),
            'add'                                    => array('RelationController', 'add'),
            '#int#'                                    => array('RelationController', 'view'),
            '#int#/edit'                            => array('RelationController', 'edit'),
            
            ...
            
            'bulk/search'                            => array('RelationMigrationController', 'bulkSearch'),
            'bulk/search/#string#'                    => array('RelationMigrationController', 'bulkSearch'),
            'bulk/search/#string#/#path#'            => array('RelationMigrationController', 'bulkSearch'),
            
        );
    }
}

When the Core processes a path like this

/relation/8119/edit

it first determines the module from the first section of path: relation. The module is mod_relation.

The remainder of the path

8119/edit

is passed to the ModuleIndex of mod_relation. This ModuleIndex then goes through all the path pattern in the url map and tries to match them against the offered path. This is done with regular expression matching. But since the patterns in the map contain user-friendly tokens like #int#, these need to be replaced first by regexp expressions, like (\d+).

The example url matches the pattern '#int#/edit' and the number 8119 matches the token #int#. The ModuleIndex then calls the static function RelationController::edit and passes the token values that were found (in this case just 8119) as distinct parameters.

RelationController::edit(8119);

This simple scheme ensures that just well-formed urls are passed to the module. It also does some minimal type checking of the arguments in the url. For example, it is not possible to pass a string value to the edit function, because it will not match the #int#. This helps to reduce application errors and hacker attacks.


Header photo: Library interior

Share this post!

Comments

Leave a comment!

Italic and bold

*This is italic*, and _so is this_.
**This is bold**, and __so is this__.

Links

This is a link to [Procurios](http://www.procurios.nl).

Lists

A bulleted list can be made with:
- Minus-signs,
+ Add-signs,
* Or an asterisk.

A numbered list can be made with:
1. List item number 1.
2. List item number 2.

Quote

The text below creates a quote:
> This is the first line.
> This is the second line.

Code

A text block with code can be created. Prefix a line with four spaces and a code-block will be made.