Utilities Helpers
Utilities helpers are the odds and ends that don't fit anywhere else. Debugging, sorting, IP addresses, file info and view rendering.
Call them whenever:
- block direct URL access to controller methods
- debug output in pretty JSON
- get the client's IP
- render views inside templates
- extract filename and extension
- sort associative arrays
- sort object arrays
- detect MX requests
Debug with JSON
The function dumps data in readable JSON format. Perfect for inspecting arrays and objects during development.
$data = ['name' => 'John', 'age' => 30];
json($data);Output:
<pre>{
"name": "John",
"age": 30
}</pre>Stop Execution
Pass true as the second argument to kill the script immediately after output:
json($data, true); // Outputs and diesGet Client IP
The function returns the visitor's IP address.
$ip = ip_address();
echo $ip; // 192.168.1.1Render Views
The function loads a view file from a module. Use it inside templates to inject content.
$data = [
'view_module' => 'products',
'view_file' => 'listing',
'products' => $products
];
display($data);This loads modules/products/views/listing.php and passes $products to it.
Auto-Detection
If you don't specify view_module, it uses the first URL segment. If you don't specify view_file, it defaults to index.
// On URL: /products/list
display($data); // Loads modules/products/views/index.phpIf the view file doesn't exist, shows a red error message with the expected path.
Extract File Info
The function splits a file path into name and extension.
$info = return_file_info('document.pdf');
echo $info['file_name']; // document
echo $info['file_extension']; // .pdfSort Arrays
The function sorts associative arrays by a specific key.
$users = [
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 25],
['name' => 'Bob', 'age' => 35]
];
sort_by_property($users, 'age');
// Sorted by age ascending
sort_by_property($users, 'name', 'desc');
// Sorted by name descendingSort Objects
The function does the same thing but for arrays of objects.
$rows = [
(object)['name' => 'John', 'age' => 30],
(object)['name' => 'Jane', 'age' => 25]
];
sort_rows_by_property($rows, 'age', 'desc');Detect Trongate MX Requests
The function checks if the current request came from Trongate MX (Trongate's JavaScript library for dynamic page updates).
if (from_trongate_mx()) {
// Return JSON or partial HTML
echo json_encode($data);
} else {
// Return full page
$this->template('standard', $data);
}Use to handle AJAX requests differently from normal page loads - no extra routing required.
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.