$input->urlSegmentStr() method

Get the string of URL segments separated by slashes

  • Note that return value lacks leading or trailing slashes.
  • URL segments must be enabled in the template settings (for template used by the page).
  • The maximum segments allowed can be adjusted in your $config->maxUrlSegments setting.
  • URL segments are populated by ProcessWire automatically on each request.
  • URL segments are already sanitized as page names.
  • The URL segment string can also be accessed by property: $input->urlSegmentStr.

Example

// Adjust output according to urlSegmentStr
// In this case our urlSegmentStr is 2 URL segments
$s = $input->urlSegmentStr();
if($s == 'photos/large') {
  // show large photos
} else if($s == 'photos/small') {
  // show small photos
} else if($s == 'map') {
  // show map
} else if(strlen($s)) {
  // something we don't recognize
  throw new Wire404Exception();
} else {
  // no URL segments present, do some default behavior
  echo $page->body;
}

Usage

// basic usage
$string = $input->urlSegmentStr();

// usage with all arguments
$string = $input->urlSegmentStr(bool $verbose = false, array $options = []);

Arguments

NameType(s)Description
verbose (optional)bool

Include pagination number (pageNum) and trailing slashes, when appropriate? (default=false)

  • Use this option for a more link-ready version of the URL segment string (since 3.0.106).
options (optional)array

Options to adjust behavior (since 3.0.106):

  • segments (array|null): Optionally specify URL segments to use, rather than those from current request. (default=null)
  • pageNum (int): Optionally specify page number to use rather than current. (default=current page number)
  • page (Page): Optionally specify Page to use for context. (default=current page)
  • NOTE the pageNum and page options are not applicable unless the $verbose argument is true.

Return value

string

URL segment string, i.e. segment1/segment2/segment3 or blank if none

See Also


$input methods and properties

API reference based on ProcessWire core version 3.0.130

Twitter updates

  • New post: Quietly and without interruption this week, the ProcessWire.com website (and all subdomains) moved from a single static server to a load-balanced multi-server environment, giving us even more horsepower and redundancy than before— More
    19 April 2019
  • New post: With ProcessWire 3.0.130, this week we’ll take a detailed look at a few useful new API additions made in the last few weeks, along with examples of each—More
    12 April 2019
  • New post: This week ProcessWire ProMailer has been released, plus we’ve got a nice upgrade in our community support forum, and more— More
    15 March 2019

Latest news

  • ProcessWire Weekly #258
    In the 258th issue of ProcessWire Weekly we'll cover a hosting upgrade for the processwire.com site and it's subdomains, introduce PW Review and Terminal module, and feature the new site of O'Neil Cinema. Read on!
    Weekly.pw / 20 April 2019
  • ProcessWire.com website hosting upgrades
    Quietly and without interruption this week, our whole website moved from a single static server to a load-balanced multi-server environment, giving us even more horsepower and redundancy than before.
    Blog / 19 April 2019
  • Subscribe to weekly ProcessWire news

“ProcessWire is like a breath of fresh air. So powerful yet simple to build with and customise, and web editors love it too.” —Margaret Chatwin, Web developer