Loading Modules
Two ways. Zero drama.
Modules can be loaded from the URL or from code. No service containers. No CLI tools. No PSR autoloaders. Just Native PHP.
1. From the URL (the easy way)
Go to:
https://yoursite.com/members/profileTrongate runs:
<?php
class Members extends Trongate {
function profile() {
// go nuts
}
}Important Trongate v2 Change!
URL segments are no longer automatically passed as method parameters. This improves security and performance.
Use the function to explicitly fetch URL parameters when you need them.
2. From Code (the fun way)
Inside any controller:
// Load and use a 'tax' module (with one line of code!)
$vat = $this->tax->calculate($amount);Inside any view, use :
<div><?= Modules::run('users/stats', $user_data) ?></div>Attention Trongate v1 veterans!!!
$this->module('tax') is dead. We murdered it in v2. There's no longer any need to write that.
You're very welcome.
- Pass ANYTHING.
The second parameter of Modules::run() accepts ANY PHP data type:
- Arrays - Pass complex datasets
- Integers/Floats - Pass numeric values
- Strings - Pass text content
- Booleans - Pass true/false flags
- Objects - Pass complete objects
- Resources - Pass file handles, database connections
- NULL - Pass nothing explicitly
One parameter. Every data type. Unlimited possibilities.
EXAMPLES
Example 1: Pass a simple string
// Pass a single string value
$result = Modules::run('email/send', '[email protected]');Example 2: Pass an integer ID
// Pass a numeric ID
$profile = Modules::run('users/get_profile', 88);Example 3: Pass a boolean flag
// Pass true/false for feature toggles
$html = Modules::run('ui/render_widget', true); // true = show advanced versionExample 4: Pass a complex array
// Pass a complete dataset as an array
$order_data = [
'order_id' => 12345,
'customer' => 'Jane Smith',
'items' => [
['id' => 1, 'qty' => 2, 'price' => 19.99],
['id' => 2, 'qty' => 1, 'price' => 49.99]
],
'shipping' => 'express'
];
$invoice = Modules::run('orders/generate_invoice', $order_data);Example 5: Pass an object
// Pass a complete object
$user = new stdClass();
$user->id = 88;
$user->name = 'John Doe';
$user->email = '[email protected]';
$welcome = Modules::run('messages/welcome_user', $user);Example 6: Pass NULL (explicitly pass nothing)
// Explicitly pass null (different from not passing anything)
$default_view = Modules::run('dashboard/load_default', null);Inside your module method:
<?php
class Users extends Trongate {
// Your method receives WHATEVER you pass
public function stats($data) {
// $data could be ANYTHING:
// - An array of user data
// - A single user ID (integer)
// - A user object
// - A boolean flag
// - NULL
// Type checking is YOUR responsibility
if (is_array($data)) {
// Handle array
} elseif (is_int($data)) {
// Handle integer ID
} elseif (is_object($data)) {
// Handle object
}
// Or just trust your code and use it directly
return "Processed data of type: " . gettype($data);
}
}
?>We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.
Share your thoughts in the Documentation Feedback.