2626 *
2727 * @internal Outside the framework this should not be used directly.
2828 *
29- * @param string $relativePath May include queries or fragments
29+ * @param string $relativePath May include queries or fragments
30+ * @param App|null $config Alternative Config to use
31+ * @param bool $useConfig Whether to use the Config baseURL
3032 *
3133 * @throws HTTPException For invalid paths.
3234 * @throws InvalidArgumentException For invalid config.
3335 */
34- function _get_uri (string $ relativePath = '' , ?App $ config = null ): URI
36+ function _get_uri (string $ relativePath = '' , ?App $ config = null , bool $ useConfig = false ): URI
3537 {
3638 $ config ??= config ('App ' );
3739
@@ -57,9 +59,13 @@ function _get_uri(string $relativePath = '', ?App $config = null): URI
5759 $ request = Services::request ();
5860
5961 /** @var App $config */
60- $ url = $ request instanceof CLIRequest
61- ? rtrim ($ config ->baseURL , '/ ' ) . '/ '
62- : $ request ->getUri ()->getBaseURL ();
62+ if ($ useConfig ) {
63+ $ url = rtrim ($ config ->baseURL , '/ ' ) . '/ ' ;
64+ } else {
65+ $ url = $ request instanceof CLIRequest
66+ ? rtrim ($ config ->baseURL , '/ ' ) . '/ '
67+ : $ request ->getUri ()->getBaseURL ();
68+ }
6369
6470 // Check for an index page
6571 if ($ config ->indexPage !== '' ) {
@@ -88,18 +94,28 @@ function _get_uri(string $relativePath = '', ?App $config = null): URI
8894 /**
8995 * Returns a site URL as defined by the App config.
9096 *
91- * @param array|string $relativePath URI string or array of URI segments
92- * @param string|null $scheme URI scheme. E.g., http, ftp
93- * @param App|null $config Alternate configuration to use
97+ * @param array|string $relativePath URI string or array of URI segments
98+ * @param string|null $scheme URI scheme. E.g., http, ftp
99+ * @param App|null $config Alternate configuration to use
100+ * @param bool $dontUseConfig Set true if you don't use the Config
101+ * baseURL even when you pass the Config
94102 */
95- function site_url ($ relativePath = '' , ?string $ scheme = null , ?App $ config = null ): string
96- {
103+ function site_url (
104+ $ relativePath = '' ,
105+ ?string $ scheme = null ,
106+ ?App $ config = null ,
107+ bool $ dontUseConfig = false
108+ ): string {
109+ // If $dontUseConfig is false and $config is not passed,
110+ // we use the Config baseURL.
111+ $ useConfig = (! $ dontUseConfig ) && ($ config !== null );
112+
97113 // Convert array of segments to a string
98114 if (is_array ($ relativePath )) {
99115 $ relativePath = implode ('/ ' , $ relativePath );
100116 }
101117
102- $ uri = _get_uri ($ relativePath , $ config );
118+ $ uri = _get_uri ($ relativePath , $ config, $ useConfig );
103119
104120 return URI ::createURIString (
105121 $ scheme ?? $ uri ->getScheme (),
@@ -124,7 +140,7 @@ function base_url($relativePath = '', ?string $scheme = null): string
124140 $ config = clone config ('App ' );
125141 $ config ->indexPage = '' ;
126142
127- return site_url ($ relativePath , $ scheme , $ config );
143+ return site_url ($ relativePath , $ scheme , $ config, true );
128144 }
129145}
130146
0 commit comments