Trongate Docs
switch to dark modeswitch to dark mode
Calling Another Module From A Controller

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:

  • a module called 'tax' for calculating how much tax needs to be paid
  • a 'shipping' module for calculating the cost of shipping
  • a 'discount' module that's dedicated to applying any relevant discounts that are available

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;

Top Tip
You'll notice, in the example above, that the three modules were loaded togetherhigh 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.


If you have a question or a comment relating to anything you've see here, please goto the Help Bar.