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

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.

PHP
echo current_url();
// https://example.com/products/view/42

Extract 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:

PHP
echo segment(1);  // products
echo segment(2);  // view
echo segment(3);  // 42

Type Casting

Pass a second argument to force the segment into a specific type:

PHP
$id = segment(3, 'int');  // Forces segment to integer

Count and Read Segments

The function counts how many segments exist after the base URL.

PHP
$count = get_num_segments();  // 3 (for /products/view/42)

The function returns the final segment.

PHP
echo get_last_segment();  // 42

Redirect Users

The function sends users to another page and stops execution immediately.

Relative Redirect

PHP
redirect('dashboard');
// Redirects to https://example.com/dashboard

Absolute Redirect

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

PHP
$referer = previous_url();

if ($referer !== '') {
    redirect($referer);
}

Build Links

The function generates HTML anchor tags with automatic URL handling and XSS protection.

Basic Link

PHP
echo anchor('about', 'About Us');
// <a href="https://example.com/about">About Us</a>

External Link

PHP
echo anchor('https://google.com', 'Search');
// <a href="https://google.com">Search</a>

Link with Attributes

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

PHP
$url = 'https://example.com/search?q=test&page=2';
echo remove_query_string($url);
// https://example.com/search

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