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

Custom Routing

Automatic routing is brilliant 99% of the time. This page is for the 1% when you want to bend URLs to your will.

Here's how it works. Open: config/custom_routing.php

Add one line. Refresh. Done.


Basic Routes – “Pretend You’re Somewhere Else”

Want /login to secretly run users/login?

PHP
<?php
$routes = [
    'login' => 'users/login'
];

define('CUSTOM_ROUTES', $routes);

Result: Address bar stays /login. User sees login form. You look clever.


Dynamic Routes? It's Wildcards Ahoy!

Need to catch IDs, slugs, or entire paths? Three wildcards. No more. No less.

Wildcard Matches Use When
(:num)123Numeric IDs
(:any)macbook-proSlugs, categories
(:all)docs/api/v2/whateverCapturing everything after
MUST BE LAST

CRITICAL RULE:

If using (:all) it must be the final segment in your pattern.

Below is an example of a custom_routing.php file that demonstrates using wildcards to map dynamic URLs to specific controllers and methods.

PHP
<?php
$routes = [
    'product/(:num)'      => 'store/item/$1',
    'laptops/(:any)'      => 'store/item/$1',
    'docs/(:all)'         => 'help/$all',     // Works
    // 'blog/(:all)/edit' => 'broken forever' // Does NOT work
];

define('CUSTOM_ROUTES', $routes);

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