PHP Luminova: Read Request URI Segments
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
CountableandStringable.
Methods
__construct
Create a collection from URI segments.
public function __construct(protected array $segments = [])Parameters:
| Parameter | Type | Description |
|---|---|---|
$segments | array<int,string> | Segments in path order. Defaults to []. |
position
Read a segment by its zero-based index.
public function position(int $index = 0): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$index | int | Zero-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(): ?stringReturn:
?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(): ?stringReturn:
?string - The same result as first().
last
Read the final segment.
public function last(): ?stringReturn:
?string - The last segment or null for an empty collection.
previous
Read the segment immediately before the last segment.
public function previous(): ?stringReturn:
?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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$position | int | Zero-based segment position. |
$segment | string | Segment 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$position | int | Zero-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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$segment | string | Segment 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(): intReturn:
int - The number of segments.
all
Read the full list.
public function all(): arrayReturn:
array<int,string> - The segments in path order.
toString
Join the segments into a path with a leading slash.
public function toString(): stringReturn:
string - The path, or / for an empty collection.
__toString
Convert the collection to a path when used as a string.
public function __toString(): stringReturn:
string - The same path as toString().