$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|bool): Specify pagination number, "+" for next pagination, "-" for previous pagination, or boolean true (3.0.155+) for current.
  • urlSegmentStr (string|bool): Specify a URL segment string to append, or true (3.0.155+) for current.
  • urlSegments (array|bool): Specify array of URL segments to append (may be used instead of urlSegmentStr), or boolean true (3.0.155+) for current. Specify associative array to use keys and values in order (3.0.155+).
  • 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.
  • host (string): Force hostname to use, i.e. 'world.com' or 'hello.world.com'. The 'http' option is implied. (3.0.178+)
  • scheme (string): Like http option, makes URL have scheme+hostname, but you specify scheme here, i.e. 'https' (3.0.178+) Note that if you specify scheme of 'https' and $config->noHTTPS is true, the 'http' scheme will still be used.

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.236

Latest news

  • ProcessWire Weekly #532
    In the 532nd issue of ProcessWire Weekly we'll cover the latest core updates, check out a couple of brand-new third party modules, and more. Read on!
    Weekly.pw / 20 July 2024
  • Page List Custom Children module
    This simple module gives you the ability to customize the parent/child relationship as it appears in the admin page list, enabling child pages to appear under more than one parent.
    Blog / 28 June 2024
  • Subscribe to weekly ProcessWire news

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK