Controller Files
Every module gets one controller file. Every controller file contains one PHP class. The boss.
Trongate v2 killed the controllers/ subfolder.
We got rid of this →
modules/users/controllers/Users.phpAnd, now we do this →
modules/users/Users.phpYou're welcome.
Where They Live Now
modules/
users/
Users.php ← the boss
views/
Users_model.php ← optionalThe text above demonstrates some file and directory locations for a hypothetical 'users' module.
Naming Rules
| Thing | Rule | Example |
|---|---|---|
| Module Directory Name | lowercase, usually plural | luxury_wristwatches |
| Controller File Name | UppercaseFirst, snake_case + .php |
Luxury_wristwatches.php |
| Class Name | UppercaseFirst, snake_case | class Luxury_wristwatches |
Bare Minimum Controller
<?php
class Dice extends Trongate {
function roll() {
echo rand(1, 6);
}
}Visit: https://yoursite.com/dice/roll → rolls a die. No config. No YAML. No drama.
Real-World Example: Users
<?php
class Users extends Trongate {
function index() {
$data['users'] = $this->db->get('id', 'desc');
$this->view('users_list', $data);
}
function profile() {
$user_id = segment(3, 'int');
$user = $this->db->get_where($user_id, 'users');
$this->view('user_profile', $user);
}
}URL → Method magic:
/users→index()/users/profile/42→profile()(usesegment(3, 'int')to fetch the ID)
Constructors? Optional.
Don't need one? Don't write one. Need CORS? Read this. Auth? Go nuts:
Never write an empty constructor just to call parent::__construct(). That's 2005. Let it go!
Multiple Classes? Don't.
Want another class? Make a child module. Not another file in the same folder.
modules/
users/
child_modules/
greetings/
Greetings.phpCopy. Paste. Works. No require_once hell.
Third-Party? Sure. But Why?
Need Guzzle? Stripe? Fine. Drop Composer in vendor/, autoload it:
require_once APPPATH.'vendor/autoload.php';
use GuzzleHttp\Client;But 99% of the time Native PHP wins. Faster. Cleaner. No 3 AM updates.
Namespaces? Only When Forced
Using Packagist? Use their PascalCase namespaces. Otherwise? Snake case everything.
One file. One class. Native PHP rules!
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.