Trongate PHP Framework Docs
Introduction
Quick Start
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
Security
Tips And Best Practices

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.php

And, now we do this →

modules/users/Users.php

You're welcome.

Where They Live Now

modules/
  users/
    Users.php          ← the boss
    views/
    Users_model.php    ← optional

The 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
<?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
<?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:

  • /usersindex()
  • /users/profile/42profile() (use segment(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.php

Copy. Paste. Works. No require_once hell.

Third-Party? Sure. But Why?

Need Guzzle? Stripe? Fine. Drop Composer in vendor/, autoload it:

PHP
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.

Leave Feedback About This Page