Header Small Ahoy!

Calling Another Module From A Controller

With the Trongate framework, there are two mechanisms by which a module can load another module.

The first method is calling a module from within a controller file. The second is calling a module from a view file. Here's how to call a module from a controller file.


As you can see, just two lines of code is all it takes to invoke a method from a different module.

Below is a hypothetical example of a method that calls upon a 'tax' module to calculate a price after tax has been added.

function _calculate_total($price) {

  //load the "tax" module

  //add tax, using the "Tax" module
  $total = $this->tax->_add_tax($price);
  return $total;

Below is an example of a method that calls upon three other modules to calculate a total price. The modules being called here will be named 'tax', 'shipping' and 'discount'. So, we're imagining:

Here's the code:

function _calculate_total($price) {

  $price = $this->tax->_add_tax($price);
  $price = $this->shipping->_add_shipping($price);
  $total = $this->discount->_apply_discount($price);
  return $total;

You'll notice, in the example above, that the three modules were loaded together, high up in the code and in alphabetical order. Strictly speaking, this was not necessary. However, the practice of loading classes in this manner has been found to be very good for code readability and maintainability.