Luminova Framework

PHP Luminova: Read Request URI Segments

Last updated: 2026-11-28 20:57:12

Inspect request URI segments in Luminova. Read segments by position, check whether the path contains a value, or retrieve all segments in order.

The Segments Object class represents the URI parts of the current request path. Accessed via Router::getSegment() when a controller or route handler needs to check part of the URI, such as its first segment.

NoteIn CLI routing, the collection contains a single framework-defined segment, __cli__.


Example

For a request to /blog/posts/42, read individual segments or retrieve the full list.

use Luminova\Routing\Router;

$segments = Router::getSegment();

echo $segments->first();     // blog
echo $segments->position(1); // posts
echo $segments->last();      // 42
echo $segments->previous();  // posts
$all = $segments->all();     // ['blog', 'posts', '42']

Class Definition

  • Full namespace: Luminova\Routing\Segments
  • This class implements Countable and Stringable.

Methods

__construct

Create a collection from URI segments.

public function __construct(protected array $segments = [])

Parameters:

ParameterTypeDescription
$segmentsarray<int,string>Segments in path order. Defaults to [].

position

Read a segment by its zero-based index.

public function position(int $index = 0): ?string

Parameters:

ParameterTypeDescription
$indexintZero-based segment index. Defaults to 0.

Return:

?string - The segment or null if the index is absent.


first

Read the first segment.

public function first(): ?string

Return:

?string - The first segment or null for an empty collection.


prefix

Read the first segment, which can be used as the URI prefix.

public function prefix(): ?string

Return:

?string - The same result as first().


last

Read the final segment.

public function last(): ?string

Return:

?string - The last segment or null for an empty collection.


previous

Read the segment immediately before the last segment.

public function previous(): ?string

Return:

?string - The previous segment or null if there are fewer than two segments.


is

Compare a segment at a position, ignoring leading and trailing slashes.

public function is(int $position, string $segment): bool

Parameters:

ParameterTypeDescription
$positionintZero-based segment position.
$segmentstringSegment text to match.

Return:

bool - Whether the segment exists and matches.


has

Check whether a segment exists at a position.

public function has(int $position): bool

Parameters:

ParameterTypeDescription
$positionintZero-based segment position.

Return:

bool - Whether the position exists. An empty string at that position counts as present.


contains

Search for a segment. Leading and trailing slashes are removed from the search value.

public function contains(string $segment): bool

Parameters:

ParameterTypeDescription
$segmentstringSegment text to match.

Return:

bool - Whether the collection contains the segment.


count

Count the segments. You can also use PHP’s count($segments).

public function count(): int

Return:

int - The number of segments.


all

Read the full list.

public function all(): array

Return:

array<int,string> - The segments in path order.


toString

Join the segments into a path with a leading slash.

public function toString(): string

Return:

string - The path, or / for an empty collection.


__toString

Convert the collection to a path when used as a string.

public function __toString(): string

Return:

string - The same path as toString().