mirror of
https://github.com/processwire/processwire.git
synced 2025-08-12 01:34:31 +02:00
Add support for scheme and host to be manually specified in $page->url() and $page->httpUrl() methods in $options array argument. This is related to the request in PR #116
This commit is contained in:
@@ -3076,6 +3076,9 @@ class Page extends WireData implements \Countable, WireMatchable {
|
||||
* - `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:
|
||||
*
|
||||
@@ -3217,21 +3220,20 @@ class Page extends WireData implements \Countable, WireMatchable {
|
||||
public function httpUrl($options = array()) {
|
||||
$template = $this->template;
|
||||
if(!$template) return '';
|
||||
/** @var Config $config */
|
||||
$config = $this->wire('config');
|
||||
if(is_array($options)) unset($options['http']);
|
||||
if($options === true || $options === false) $options = array();
|
||||
$url = $this->url($options);
|
||||
if(strpos($url, '://')) return $url;
|
||||
$config = $this->wire()->config;
|
||||
$mode = $template->https;
|
||||
if($mode > 0 && $config->noHTTPS) $mode = 0;
|
||||
switch($mode) {
|
||||
case -1: $protocol = 'http'; break;
|
||||
case 1: $protocol = 'https'; break;
|
||||
default: $protocol = $config->https ? 'https' : 'http';
|
||||
case -1: $scheme = 'http'; break;
|
||||
case 1: $scheme = 'https'; break;
|
||||
default: $scheme = $config->https ? 'https' : 'http';
|
||||
}
|
||||
if(is_array($options)) {
|
||||
unset($options['http']);
|
||||
} else if(is_bool($options)) {
|
||||
$options = array();
|
||||
}
|
||||
return "$protocol://" . $config->httpHost . $this->url($options);
|
||||
$url = "$scheme://$config->httpHost$url";
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -580,6 +580,8 @@ class PageTraversal {
|
||||
* Specify associative array (in 3.0.155+) to make both keys and values part of the URL segment string.
|
||||
* - `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).
|
||||
* - `scheme` (string): Like the http option, makes URL include scheme and hostname, but you specify scheme with this, i.e. 'https' (3.0.178+)
|
||||
* - `host` (string): Hostname to force use of, i.e. 'world.com' or 'hello.world.com'. The 'http' option is implied when host specified. (3.0.178+)
|
||||
* - `language` (Language): Specify Language object to return URL in that Language.
|
||||
*
|
||||
* You can also specify any of the following for `$options` as shortcuts:
|
||||
@@ -611,6 +613,8 @@ class PageTraversal {
|
||||
|
||||
$defaults = array(
|
||||
'http' => is_bool($options) ? $options : false,
|
||||
'scheme' => '',
|
||||
'host' => '',
|
||||
'pageNum' => is_int($options) || (is_string($options) && in_array($options, array('+', '-'))) ? $options : 1,
|
||||
'data' => array(),
|
||||
'urlSegmentStr' => is_string($options) ? $options : '',
|
||||
@@ -704,13 +708,22 @@ class PageTraversal {
|
||||
$url .= '?' . $query;
|
||||
}
|
||||
|
||||
if($options['http']) {
|
||||
switch($template->https) {
|
||||
if($options['scheme']) {
|
||||
$scheme = strtolower($options['scheme']);
|
||||
if(strpos($scheme, '://') === false) $scheme .= '://';
|
||||
if($scheme === 'https://' && $config->noHTTPS) $scheme = 'http://';
|
||||
$host = $options['host'] ? $options['host'] : $config->httpHost;
|
||||
$url = "$scheme$host$url";
|
||||
|
||||
} else if($options['http'] || $options['host']) {
|
||||
$mode = $config->noHTTPS ? -1 : $template->https;
|
||||
switch($mode) {
|
||||
case -1: $scheme = 'http'; break;
|
||||
case 1: $scheme = 'https'; break;
|
||||
default: $scheme = $config->https ? 'https' : 'http';
|
||||
}
|
||||
$url = "$scheme://" . $page->wire('config')->httpHost . $url;
|
||||
$host = $options['host'] ? $options['host'] : $config->httpHost;
|
||||
$url = "$scheme://$host$url";
|
||||
}
|
||||
|
||||
return $url;
|
||||
|
Reference in New Issue
Block a user