5.1. Modules

5.1.5. Invoking Modules

In Trex, modules are objects and can be accessed from anywhere within a project. The are two ways of accessing a module, using the getModule() or requestModule() method. Both methods return a single instance of the module.

  • obj getModule ( string module_name )
  • obj requestModule ( string module_name [, array request_url] )

Example: Accessing the “articles” Module

$obj = $this->getModule("articles");

The requestModule() method can also simulate an HTTP request, passing to the module an action and overwriting the URL parameters.

Example: Simulating an HTTP request

$templateObject = $this->requestModule("articles", array("action", "param1", "param2"));

That same module request could be made through the browser by accessing the following URL:


There are several conventions that a module must follow in order to be loaded correctly:

  • Modules must be located in the app/modules/ directory, and the module directory name must correspond to the “name” attribute of that module: e.g. “articles” module is expected to be found at app/modules/articles/.
  • Modules must have a front-end Action Controller.
  • Actions are expected to correspond to class methods, e.g.: actionIndex().

Example: Invoking modules

class News extends Trex_Module 
        public function actionIndex()
                // Loads and executes the Grid module. Returns a Template object.
                $templateObject = $this->requestModule("grid"); 

                // Loads, executes and renders the Grid module. Returns a string.
                $templateString = $this->requestModule("grid")->render();

                // Loads the Grid module and simulates an HTTP request.
                $templateObject = $this->requestModule("grid", array("actionDisplay", "param1"));

                // Calls a method of the Grid module.
                $templateObject = $this->getModule("grid")->actionIndex();

5.1.6. Sharing data between Modules

Using the getModule() method:

$instance = $this->getModule("grid");
$string = $this->getModule("grid")->getModuleName();

Using the getModel() method:

$instance = $this->getModel("grid");
$rs = $this->getModel("grid")->selectItems();
$row = $this->getModel("grid")->selectItems()->getRow();

Using the requestModule() method:

// Loads the Grid module and calls the actionIndex() method.
$templateObject = $this->requestModule("grid");

// Loads the Grid module and calls the actionDisplay() method.
$templateObject = $this->requestModule("grid", array("actionDisplay", "param1", "param2"));

In the example above, the requestModule() method loads the “grid” module and calls the actionDisplay() method of the front-end Action Controller. The first argument is a string containing the name of the module, the second argument is optional and holds the URL array. If the second argument is passed, then the URL path will be overwritten (this includes the action and the parameters).

5.1.7. Including other files

Because Trex makes it easy to write good, modular code, you’ll often find yourself producing small files containing some chunk of self-contained functionality, an object to do x or a function to do y. Having produced these files, you’ll want to incorporate them into your module. This can be done by calling the loadFile() method.

Example: Including files

public function actionIndex()
        // Including a single file
        $this->loadFile("class.php", "/path/to/file");

5.1.8. Generators

With a single command, you can create an entire module based on the app/modules/_skeleton/ directory. Open up a shell, go to the app/modules/ directory and type:

$ ./addmodule -mt -n news-test -N NewsTest


$ ./addmodule -h

Usage: addmodule [-hacirmsl] -n <module_name> -N <class_name>
     -a, create actions/ directory
     -c, create config/ directory
     -i, create includes/ directory
     -r, create public/ directory
     -m, create models/ directory
     -s, create setup/ directory
     -n, name of the module (lower-case)
     -N, name of the class (camel-case)
     -h, usage and options (this help)
     -l, see this script

Note: If you don’t have access to the shell, you can create a copy of the app/modules/_skeleton/default/ directory manually.

Pages: 1 2