Header Small Ahoy!

Custom URL Routing in Trongate

Introduction to Custom Routing

Custom URL routing in Trongate allows developers to define user-friendly, intuitive URL patterns that map to specific controllers and methods. This feature enhances application usability and SEO performance while maintaining clean code structure.

Configuration File

Custom routes are defined in the custom_routing.php file, located in the 'config' directory. This file contains an array of key-value pairs, where:

Default Custom Routes

A fresh Trongate installation includes predefined custom routes. Here's the default content of custom_routing.php:

<?php
$routes = [
    'tg-admin' => 'trongate_administrators/login',
    'tg-admin/submit_login' => 'trongate_administrators/submit_login',
    'trongate-pages' => 'trongate_pages/index'
];
define('CUSTOM_ROUTES', $routes);

Understanding Default Routes

The following table provides an overview of the default custom routes in Trongate - showing how specific URL patterns are mapped to corresponding controller methods.

Route Controller/Method Purpose
/tg-admin trongate_administrators/login Provides a user-friendly URL for the admin login page
/tg-admin/submit_login trongate_administrators/submit_login Handles admin login form submissions
/trongate-pages trongate_pages/index Maps hyphenated URLs, relating to 'Trongate Pages', to the 'Trongate Pages' module.

Creating Custom Routes

Basic Custom Routing

To create a simple custom route, add a key-value pair to the $routes array:

<?php
$routes = [
    'login' => 'users/login'
];
define('CUSTOM_ROUTES', $routes);

This example redirects https://example.com/login to the login method of the users module, without changing the URL visible in the browser.

Advanced Routing with Wildcards

Trongate supports dynamic URL parameters using wildcards:

Example of wildcard usage:

<?php
$routes = [
    'product/(:num)' => 'catalog/item_details/$1',
    'profile/(:any)' => 'users/show_profile/$1'
];
define('CUSTOM_ROUTES', $routes);

In these examples:

Benefits of Custom Routing

By leveraging Trongate's custom routing capabilities, developers can create more intuitive, maintainable, and SEO-friendly web applications.