Trongate PHP Framework Docs
Introduction
Basic Concepts
Understanding Routing
Intercepting Requests
Module Fundamentals
Database Operations
Templates
Helpers
Form Handling
Form Validation
Working With Files
Image Manipulation
Working With Dates & Times
Language Control
Authorization & Authentication
Tips And Best Practices

Building Modules: Controllers vs Utilities

One of Trongate v2's most powerful features is its ability to intelligently handle both framework-dependent controllers and standalone utility classes. This page explains when to use each pattern and how the framework automatically adapts.

The Two Types of Modules

In Trongate v2, modules fall into two categories:

Controllers (Extend Trongate)

Controllers are modules that need framework features like database access, view rendering, or loading other modules.

Utilities (Standalone Classes)

Utilities are self-contained classes that perform specific tasks without needing any framework features.

When to Extend Trongate

Your module should extend Trongate if it needs any of these features:

  • Database operations - $this->model
  • View rendering - $this->view()
  • Loading other modules - $this->module('name') or $this->modulename
  • Module name tracking - $this->module_name

Common Controller Examples

  • Products - manages product data, displays views
  • Users - handles user accounts, authentication
  • Orders - processes orders, generates invoices
  • Admin - dashboard, reports, management interface
  • Api - endpoints that query databases and return JSON

When NOT to Extend Trongate

Create a standalone utility class if your module:

  • Performs pure computation - math, string manipulation, data formatting
  • Wraps external libraries - image processing (GD), PDFs, email
  • Has no database needs - doesn't read or write data
  • Has no view rendering - doesn't display HTML
  • Could be used outside Trongate - framework-agnostic functionality

Common Utility Examples

  • Image - resize, crop, manipulate images with GD
  • Pdf - generate or manipulate PDF documents
  • Calculator - perform complex calculations
  • Converter - convert between units, formats, currencies
  • Validator - custom validation logic (credit cards, phone numbers)
  • Encryptor - encryption/decryption utilities

How Trongate Handles Both Types

Here's the brilliant part: you can use both types of modules exactly the same way in your controllers.

Using a Controller Module

Using a Utility Module

Notice: The syntax is identical! You don't need to know or care whether a module extends Trongate or not.

The Framework's Intelligence

When you access $this->image, here's what happens behind the scenes:

The framework automatically detects whether a module extends Trongate and instantiates it correctly.

Zero Configuration: You don't register modules as "controllers" or "utilities". The framework figures it out automatically by checking the class inheritance.

Real-World Example: Image Utility

Let's look at a complete example of a standalone utility class:

Notice what this class doesn't have:

  • No extends Trongate
  • No $this->model usage
  • No $this->view() calls
  • No framework dependencies at all

It's pure functionality. And it works perfectly when loaded via $this->image in any controller.

Using the Image Utility in a Controller

See how natural it is? The Image utility handles image processing, while the Products controller handles business logic, database, and views.

Constructor Differences

Controllers (Extending Trongate)

Utilities (Standalone)

Important: Standalone utilities should NOT accept $module_name as a parameter. The framework instantiates them without parameters.

Performance Benefits of Standalone Utilities

Creating standalone utilities has real performance advantages:

Lower Memory Usage

Standalone classes don't carry the framework's base class overhead:

  • No $instances cache array
  • No $loaded_modules cache array
  • No $module_name, $parent_module, $child_module properties

Faster Instantiation

The framework doesn't need to:

  • Call parent::__construct()
  • Set module name properties
  • Initialize framework features

Framework-Agnostic

Standalone utilities can be:

  • Reused in other projects
  • Tested independently
  • Shared as libraries
  • Used outside of Trongate entirely

Speed Priority: For Trongate v2's goal of being the fastest PHP framework, creating standalone utilities for pure functionality eliminates unnecessary overhead.

Migration Decision Tree

When creating a new module, ask yourself:

Converting Between Types

You can easily convert between controller and utility if you change your mind:

From Controller to Utility

From Utility to Controller

Common Patterns

Pattern 1: Controller Uses Multiple Utilities

Pattern 2: Utility Uses Another Utility

Utilities can use other utilities, but they load them differently (no $this-> magic):

Or, convert Pdf to extend Trongate if it needs multiple utilities:

Testing Your Module Type

To verify you've chosen the right pattern:

Test 1: Can it work standalone?

Test 2: Does it need framework features?

Best Practices Summary

Controllers (extend Trongate):

  • Handle business logic and user interactions
  • Need database, views, or other modules
  • Must call parent::__construct($module_name) if they have a constructor
  • Examples: Products, Users, Admin, Orders

Utilities (standalone):

  • Perform specific, self-contained tasks
  • No database, views, or framework dependencies
  • Constructor accepts task-specific parameters only
  • Examples: Image, Calculator, Pdf, Validator

Advanced: Hybrid Approach

Some modules might need BOTH a controller interface AND utility functions:

Load the helper in your controller:

Conclusion

Trongate v2's dual module system gives you:

  • Flexibility - Choose the right pattern for each module
  • Performance - Zero framework overhead for utilities
  • Simplicity - Use both types identically in controllers
  • Intelligence - Framework auto-detects and adapts

Understanding when to extend Trongate vs creating standalone utilities is key to building fast, maintainable applications.

Remember: When in doubt, start with a standalone utility. You can always add extends Trongate later if you need framework features. It's easier to add framework capabilities than remove them.