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:
- Keys represent the desired URL paths
- Values specify the corresponding module and method to be invoked
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 |
|---|---|
tg-admin |
trongate_administrators/login |
tg-admin/submit_login |
trongate_administrators/submit_login |
trongate-pages |
trongate_pages/index |
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:
:num- Matches numeric segments (e.g., IDs):any- Matches any characters up to the next slash
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:
https://example.com/product/123routes tocatalog/item_details/123https://example.com/profile/johnroutes tousers/show_profile/john
Benefits of Custom Routing
- Improves URL readability and user experience
- Enhances SEO by creating meaningful URL structures
- Allows flexibility in URL design without altering application logic
- Facilitates easier management of complex URL patterns
By leveraging Trongate's custom routing capabilities, developers can create more intuitive, maintainable, and SEO-friendly web applications.