URL Helpers
URL helpers handle everything related to URLs: getting segments, building links, redirecting users and reading the current page address.
Zero config. Just call them:
- returns the full URL
- extracts URL segments
- counts segments
- returns the final segment
- sends users elsewhere
- gets the referrer
- builds HTML links
- strips query params
Get the Current URL
The function returns the full URL of the page you're on.
echo current_url();
// https://example.com/products/view/42Extract URL Segments
The function grabs a specific part of the URL. Segments are numbered starting from 1.
For the URL https://example.com/products/view/42:
echo segment(1); // products
echo segment(2); // view
echo segment(3); // 42Type Casting
Pass a second argument to force the segment into a specific type:
$id = segment(3, 'int'); // Forces segment to integerCount and Read Segments
The function counts how many segments exist after the base URL.
$count = get_num_segments(); // 3 (for /products/view/42)The function returns the final segment.
echo get_last_segment(); // 42Redirect Users
The function sends users to another page and stops execution immediately.
Relative Redirect
redirect('dashboard');
// Redirects to https://example.com/dashboardAbsolute Redirect
redirect('https://google.com');The function calls die() automatically. Nothing after it executes.
Get the Previous URL
The function returns the referrer (the page the user came from).
$referer = previous_url();
if ($referer !== '') {
redirect($referer);
}Build Links
The function generates HTML anchor tags with automatic URL handling and XSS protection.
Basic Link
echo anchor('about', 'About Us');
// <a href="https://example.com/about">About Us</a>External Link
echo anchor('https://google.com', 'Search');
// <a href="https://google.com">Search</a>Link with Attributes
$attr = ['class' => 'btn', 'target' => '_blank'];
echo anchor('contact', 'Get in Touch', $attr);
// <a href="https://example.com/contact" class="btn" target="_blank">Get in Touch</a>The function escapes URLs and attributes automatically. You don't have to.
Remove Query Strings
The function strips everything after the ? in a URL.
$url = 'https://example.com/search?q=test&page=2';
echo remove_query_string($url);
// https://example.com/searchWe'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.