URL segments are the parts of a URL separated by slashes. When building the URL, Scriptor converts each page slug in the tree into a segment. Each published page has a corresponding URL segment.
Let's say we have a URL https://my-site.com/blog/my-post
then blog
would be segment #1 and my-post
would be URL segment #2.
Page-based routing is the default behavior of Scriptor. If you want even more control over routing, use the SimpleRouter module.
How to use URL segments
URL segments can be accessed from the $urlSegments
variable or Site::urlSegments()
method.
$segments = Scriptor::getSite()->urlSegments();
You can access the single segment items with a get()
method.
// https://my-site.com/blog/my-post
$first = $segments->get(0); // blog
$second = $segments->get(1); // my-post
Get the last segment:
$last = $segments->getLast();
To get a URI path, use this method:
echo $segments->getUrl(); // blog/my-post
You can also set or replace one or more segments.
$segments->set($id, $value);
// usage example
// if ($segments->get(0) == 'given') {
// $segments->set(0, 'replacement');
// }
For example, you can customize your route controller:
if ($segments->get(0) == 'gallery') {
// image gallery
} else if ($input->get(0) == 'blog') {
// display blog ...
} else {
$site->throw404(); // display 404 page
}