$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.135

Twitter updates

  • New post: Version 3.0.135 of ProcessWire on the dev branch focuses on some .htaccess updates, adds clarity to debug mode, and improves upon the installer— More
    5 July 2019
  • ProcessWire 3.0.133 adds a useful new Page::meta() method for a new type of page-specific persistent data storage, adds the ability for users to create their own bookmarks in Lister, and has a handy and time saving update for the asmSelect input type— More
    14 June 2019
  • New post: This week we’ll take a look at 3 different WEBP image strategies that you can use in ProcessWire 3.0.132+. Then we’ll dive into a major update for the Google Client API module, and finish up by outlining some useful new updates in FormBuilder— More
    31 May 2019

Latest news

  • ProcessWire Weekly #270
    This week in the 270th issue of ProcessWire Weekly we're going to cover the SearchEngine module, feature some recent support forum highlights, and introduce a brand new site of the week. Read on!
    Weekly.pw / 13 July 2019
  • ProcessWire 3.0.135 core updates
    Version 3.0.135 of ProcessWire on the dev branch focuses on .htaccess updates, adds additional layers of security, adds clarity to debug mode, and improves upon the installer.
    Blog / 3 July 2019
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits