Skip to content

Commit a65b1c7

Browse files
authored
Merge pull request #8512 from kenjis/docs-base-url-protocol-relative
docs: add undocumented `base_url()` protocol-relative link
2 parents 2930714 + c2cbf05 commit a65b1c7

5 files changed

Lines changed: 33 additions & 15 deletions

File tree

system/HTTP/SiteURI.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,10 @@ protected function applyParts(array $parts): void
369369
/**
370370
* For base_url() helper.
371371
*
372-
* @param array|string $relativePath URI string or array of URI segments
373-
* @param string|null $scheme URI scheme. E.g., http, ftp
372+
* @param array|string $relativePath URI string or array of URI segments.
373+
* @param string|null $scheme URI scheme. E.g., http, ftp. If empty
374+
* string '' is set, a protocol-relative
375+
* link is returned.
374376
*/
375377
public function baseUrl($relativePath = '', ?string $scheme = null): string
376378
{
@@ -406,9 +408,11 @@ private function stringifyRelativePath($relativePath): string
406408
/**
407409
* For site_url() helper.
408410
*
409-
* @param array|string $relativePath URI string or array of URI segments
410-
* @param string|null $scheme URI scheme. E.g., http, ftp
411-
* @param App|null $config Alternate configuration to use
411+
* @param array|string $relativePath URI string or array of URI segments.
412+
* @param string|null $scheme URI scheme. E.g., http, ftp. If empty
413+
* string '' is set, a protocol-relative
414+
* link is returned.
415+
* @param App|null $config Alternate configuration to use.
412416
*/
413417
public function siteUrl($relativePath = '', ?string $scheme = null, ?App $config = null): string
414418
{

system/Helpers/url_helper.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
/**
2424
* Returns a site URL as defined by the App config.
2525
*
26-
* @param array|string $relativePath URI string or array of URI segments
27-
* @param string|null $scheme URI scheme. E.g., http, ftp
28-
* @param App|null $config Alternate configuration to use
26+
* @param array|string $relativePath URI string or array of URI segments.
27+
* @param string|null $scheme URI scheme. E.g., http, ftp. If empty
28+
* string '' is set, a protocol-relative
29+
* link is returned.
30+
* @param App|null $config Alternate configuration to use.
2931
*/
3032
function site_url($relativePath = '', ?string $scheme = null, ?App $config = null): string
3133
{
@@ -42,8 +44,10 @@ function site_url($relativePath = '', ?string $scheme = null, ?App $config = nul
4244
* Returns the base URL as defined by the App config.
4345
* Base URLs are trimmed site URLs without the index page.
4446
*
45-
* @param array|string $relativePath URI string or array of URI segments
46-
* @param string|null $scheme URI scheme. E.g., http, ftp
47+
* @param array|string $relativePath URI string or array of URI segments.
48+
* @param string|null $scheme URI scheme. E.g., http, ftp. If empty
49+
* string '' is set, a protocol-relative
50+
* link is returned.
4751
*/
4852
function base_url($relativePath = '', ?string $scheme = null): string
4953
{

user_guide_src/source/helpers/url_helper.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ The following functions are available:
2020

2121
.. php:function:: site_url([$uri = ''[, $protocol = null[, $altConfig = null]]])
2222
23-
:param array|string $uri: URI string or array of URI segments
24-
:param string $protocol: Protocol, e.g., 'http' or 'https'
25-
:param \\Config\\App $altConfig: Alternate configuration to use
23+
:param array|string $uri: URI string or array of URI segments.
24+
:param string $protocol: Protocol, e.g., 'http' or 'https'. If empty string '' is set, a protocol-relative link is returned.
25+
:param \\Config\\App $altConfig: Alternate configuration to use.
2626
:returns: Site URL
2727
:rtype: string
2828

@@ -56,8 +56,8 @@ The following functions are available:
5656

5757
.. php:function:: base_url([$uri = ''[, $protocol = null]])
5858
59-
:param array|string $uri: URI string or array of URI segments
60-
:param string $protocol: Protocol, e.g., 'http' or 'https'
59+
:param array|string $uri: URI string or array of URI segments.
60+
:param string $protocol: Protocol, e.g., 'http' or 'https'. If empty string '' is set, a protocol-relative link is returned.
6161
:returns: Base URL
6262
:rtype: string
6363

@@ -83,6 +83,11 @@ The following functions are available:
8383
The above example would return something like:
8484
**http://example.com/blog/post/123**
8585

86+
If you pass an empty string ``''`` as the second parameter, it returns
87+
the protocol-relative link:
88+
89+
.. literalinclude:: url_helper/026.php
90+
8691
This is useful because unlike :php:func:`site_url()`, you can supply a
8792
string to a file, such as an image or stylesheet. For example:
8893

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?php
22

3+
// Returns like `http://example.com/blog/post/123`
34
echo base_url('blog/post/123');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// Returns like `//example.com/blog/post/123`
4+
echo base_url('blog/post/123', '');

0 commit comments

Comments
 (0)