1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 17:54:44 +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:
Ryan Cramer
2021-05-13 09:56:48 -04:00
parent 108ee2384a
commit 5a5d4e5830
2 changed files with 29 additions and 14 deletions

View File

@@ -3076,6 +3076,9 @@ class Page extends WireData implements \Countable, WireMatchable {
* - `data` (array): Array of key=value variables to form a query 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). * - `http` (bool): Specify true to make URL include scheme and hostname (default=false).
* - `language` (Language): Specify Language object to return URL in that Language. * - `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: * 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()) { public function httpUrl($options = array()) {
$template = $this->template; $template = $this->template;
if(!$template) return ''; if(!$template) return '';
/** @var Config $config */ if(is_array($options)) unset($options['http']);
$config = $this->wire('config'); if($options === true || $options === false) $options = array();
$url = $this->url($options);
if(strpos($url, '://')) return $url;
$config = $this->wire()->config;
$mode = $template->https; $mode = $template->https;
if($mode > 0 && $config->noHTTPS) $mode = 0; if($mode > 0 && $config->noHTTPS) $mode = 0;
switch($mode) { switch($mode) {
case -1: $protocol = 'http'; break; case -1: $scheme = 'http'; break;
case 1: $protocol = 'https'; break; case 1: $scheme = 'https'; break;
default: $protocol = $config->https ? 'https' : 'http'; default: $scheme = $config->https ? 'https' : 'http';
} }
if(is_array($options)) { $url = "$scheme://$config->httpHost$url";
unset($options['http']); return $url;
} else if(is_bool($options)) {
$options = array();
}
return "$protocol://" . $config->httpHost . $this->url($options);
} }
/** /**

View File

@@ -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. * 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. * - `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). * - `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. * - `language` (Language): Specify Language object to return URL in that Language.
* *
* You can also specify any of the following for `$options` as shortcuts: * You can also specify any of the following for `$options` as shortcuts:
@@ -611,6 +613,8 @@ class PageTraversal {
$defaults = array( $defaults = array(
'http' => is_bool($options) ? $options : false, 'http' => is_bool($options) ? $options : false,
'scheme' => '',
'host' => '',
'pageNum' => is_int($options) || (is_string($options) && in_array($options, array('+', '-'))) ? $options : 1, 'pageNum' => is_int($options) || (is_string($options) && in_array($options, array('+', '-'))) ? $options : 1,
'data' => array(), 'data' => array(),
'urlSegmentStr' => is_string($options) ? $options : '', 'urlSegmentStr' => is_string($options) ? $options : '',
@@ -704,13 +708,22 @@ class PageTraversal {
$url .= '?' . $query; $url .= '?' . $query;
} }
if($options['http']) { if($options['scheme']) {
switch($template->https) { $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 = 'http'; break;
case 1: $scheme = 'https'; break; case 1: $scheme = 'https'; break;
default: $scheme = $config->https ? 'https' : 'http'; 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; return $url;