$page->url() method

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, int, string, bool, Language, 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.137

Twitter updates

  • This week’s dev branch version brings you improvements to ProcessWire’s input->cookie API variable, plus it adds the ability to modify system URLs and paths at runtime. This post also includes some examples to demonstrate just how useful this can be— More
    20 September 2019
  • New post: This week we’ll take a look at a new version of FormBuilder that's on the way (with a screencast), as well as the latest version of the core: ProcessWire 3.0.140— More
    6 September 2019
  • We added WebP image support this year and many have been adopting it in existing projects. We look at a process used for migrating existing websites to WebP images— from preliminary considerations to implementation & testing, w/lots of tips & tricks too: More
    16 August 2019

Latest news

  • ProcessWire Weekly #279
    This week in the 279th issue of ProcessWire Weekly we're going to introduce some of the latest third party modules out there, and highlight a brand new site of the week. Read on!
    Weekly.pw / 14 September 2019
  • ProcessWire 3.0.141 core updates
    This week’s dev branch version brings you improvements to ProcessWire’s $input->cookie API variable, adds a new hook, and now lets you modify system URLs and paths at runtime, along with a few examples to demonstrate just how useful this can be. 
    Blog / 20 September 2019
  • Subscribe to weekly ProcessWire news

“The end client and designer love the ease at which they can update the website. Training beyond how to log in wasn’t even necessary since ProcessWire’s default interface is straightforward.” —Jonathan Lahijani