Returns the URL to the page (optionally with additional $options)

  • This method can also be accessed by property $page->url (without parenthesis).

  • Like $page->path() but comes from server document root. Path and url are identical if installation is not running from a subdirectory.

  • Use $page->httpUrl() if you need the URL to include scheme and hostname.

  • Need to hook this method? While it's not directly hookable, it does use the $page->path() method, which is hookable. As a result, you can affect the output of the url() method by hooking the path() method instead.

$options argument

You can specify an $options argument to this method with any of the following:

  • pageNum (int|string): Specify pagination number, or "+" for next pagination, or "-" for previous pagination.
  • urlSegmentStr (string): Specify a URL segment string to append.
  • urlSegments (array): Specify array of URL segments to append (may be used instead of urlSegmentStr).
  • data (array): Array of key=value variables to form a query string.
  • http (bool): Specify true to make URL include scheme and hostname (default=false).
  • language (Language): Specify Language object to return URL in that Language.

You can also specify any of the following for $options as shortcuts:

  • If you specify an int for options it is assumed to be the pageNum option.
  • If you specify + or - for options it is assumed to be the pageNum “next/previous pagination” option.
  • If you specify any other string for options it is assumed to be the urlSegmentStr option.
  • If you specify a boolean (true) for options it is assumed to be the http option.

Please also note regarding $options:

  • This method honors template slash settings for page, URL segments and page numbers.
  • Any passed in URL segments are automatically sanitized with Sanitizer::pageNameUTF8().
  • If using the pageNum or URL segment options please also make sure these are enabled on the page’s template.
  • The query string generated by any data variables is entity encoded when output formatting is on.
  • The language option requires that the LanguageSupportPageNames module is installed.
  • The prefix for page numbers honors $config->pageNumUrlPrefix and multi-language prefixes as well.

Examples

// Using $page->url to output navigation
foreach($page->children as $child) {
  echo "<li><a href='$child->url'>$child->title</a></li>";
}
// Difference between url() and path() on site running from subdirectory /my-site/
echo $page->url();  // outputs: /my-site/about/contact/
echo $page->path(); // outputs: /about/contact/
// Specify that you want a specific pagination (output: /example/page2)
echo $page->url(2);

// Get URL for next and previous pagination
echo $page->url('+'); // next
echo $page->url('-'); // prev

// Get a URL with scheme and hostname (output: http://domain.com/example/)
echo $page->url(true);

// Specify a URL segment string (output: /example/photos/1)
echo $page->url('photos/1');

// Use a URL segment array (output: /example/photos/1)
echo $page->url([
  'urlSegments' => [ 'photos', '1' ]
]);

// Get URL in a specific language
$fr = $languages->get('fr');
echo $page->url($fr);

// Include data/query vars (output: /example/?action=view&type=photos)
echo $page->url([
  'data' => [
    'action' => 'view',
    'type' => 'photos'
  ]
]);

// Specify multiple options (output: http://domain.com/example/foo/page3?bar=baz)
echo $page->url([
  'http' => true,
  'pageNum' => 3,
  'urlSegmentStr' => 'foo',
  'data' => [ 'bar' => 'baz' ]
]);

Usage

// basic usage
$string = $page->url();

// usage with all arguments
$string = $page->url($options = null);

Arguments

NameType(s)Description
$options (optional)array or int or string or bool or Language or null

Optionally specify options to modify default behavior (see method description).

Return value

string

Returns page URL, for example: /my-site/about/contact/

See Also


$page methods and properties

API reference based on ProcessWire core version 3.0.78