2626 *
2727 * @internal Outside the framework this should not be used directly.
2828 *
29- * @param string $relativePath May include queries or fragments
29+ * @param array|string $relativePath URI string or array of URI segments.
30+ * May include queries or fragments.
31+ * @param App|null $config Alternative Config to use
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 ($ relativePath = '' , ?App $ config = null ): URI
3537 {
36- $ config ??= config ('App ' );
38+ $ appConfig = null ;
39+ if ($ config === null ) {
40+ /** @var App $appConfig */
41+ $ appConfig = config ('App ' );
42+
43+ if ($ appConfig ->baseURL === '' ) {
44+ throw new InvalidArgumentException (
45+ '_get_uri() requires a valid baseURL. '
46+ );
47+ }
48+ } elseif ($ config ->baseURL === '' ) {
49+ throw new InvalidArgumentException (
50+ '_get_uri() requires a valid baseURL. '
51+ );
52+ }
3753
38- if ($ config ->baseURL === '' ) {
39- throw new InvalidArgumentException ('_get_uri() requires a valid baseURL. ' );
54+ // Convert array of segments to a string
55+ if (is_array ($ relativePath )) {
56+ $ relativePath = implode ('/ ' , $ relativePath );
4057 }
4158
4259 // If a full URI was passed then convert it
@@ -53,27 +70,31 @@ function _get_uri(string $relativePath = '', ?App $config = null): URI
5370
5471 $ relativePath = URI ::removeDotSegments ($ relativePath );
5572
56- // Build the full URL based on $config and $relativePath
5773 $ request = Services::request ();
5874
59- /** @var App $config */
60- $ url = $ request instanceof CLIRequest
61- ? rtrim ($ config ->baseURL , '/ ' ) . '/ '
62- : $ request ->getUri ()->getBaseURL ();
75+ if ($ config === null ) {
76+ $ baseURL = $ request instanceof CLIRequest
77+ ? rtrim ($ appConfig ->baseURL , '/ ' ) . '/ '
78+ // Use the current baseURL for multiple domain support
79+ : $ request ->getUri ()->getBaseURL ();
80+
81+ $ config = $ appConfig ;
82+ } else {
83+ $ baseURL = rtrim ($ config ->baseURL , '/ ' ) . '/ ' ;
84+ }
6385
6486 // Check for an index page
87+ $ indexPage = '' ;
6588 if ($ config ->indexPage !== '' ) {
66- $ url . = $ config ->indexPage ;
89+ $ indexPage = $ config ->indexPage ;
6790
6891 // Check if we need a separator
6992 if ($ relativePath !== '' && $ relativePath [0 ] !== '/ ' && $ relativePath [0 ] !== '? ' ) {
70- $ url .= '/ ' ;
93+ $ indexPage .= '/ ' ;
7194 }
7295 }
7396
74- $ url .= $ relativePath ;
75-
76- $ uri = new URI ($ url );
97+ $ uri = new URI ($ baseURL . $ indexPage . $ relativePath );
7798
7899 // Check if the baseURL scheme needs to be coerced into its secure version
79100 if ($ config ->forceGlobalSecureRequests && $ uri ->getScheme () === 'http ' ) {
@@ -94,11 +115,6 @@ function _get_uri(string $relativePath = '', ?App $config = null): URI
94115 */
95116 function site_url ($ relativePath = '' , ?string $ scheme = null , ?App $ config = null ): string
96117 {
97- // Convert array of segments to a string
98- if (is_array ($ relativePath )) {
99- $ relativePath = implode ('/ ' , $ relativePath );
100- }
101-
102118 $ uri = _get_uri ($ relativePath , $ config );
103119
104120 return URI ::createURIString (
@@ -121,7 +137,15 @@ function site_url($relativePath = '', ?string $scheme = null, ?App $config = nul
121137 */
122138 function base_url ($ relativePath = '' , ?string $ scheme = null ): string
123139 {
124- $ config = clone config ('App ' );
140+ /** @var App $config */
141+ $ config = clone config ('App ' );
142+
143+ // Use the current baseURL for multiple domain support
144+ $ request = Services::request ();
145+ $ config ->baseURL = $ request instanceof CLIRequest
146+ ? rtrim ($ config ->baseURL , '/ ' ) . '/ '
147+ : $ request ->getUri ()->getBaseURL ();
148+
125149 $ config ->indexPage = '' ;
126150
127151 return site_url ($ relativePath , $ scheme , $ config );
@@ -131,27 +155,29 @@ function base_url($relativePath = '', ?string $scheme = null): string
131155if (! function_exists ('current_url ' )) {
132156 /**
133157 * Returns the current full URL based on the Config\App settings and IncomingRequest.
134- * String returns ignore query and fragment parts.
135158 *
136159 * @param bool $returnObject True to return an object instead of a string
137160 * @param IncomingRequest|null $request A request to use when retrieving the path
138161 *
139- * @return string|URI
162+ * @return string|URI When returning string, the query and fragment parts are removed.
163+ * When returning URI, the query and fragment parts are preserved.
140164 */
141165 function current_url (bool $ returnObject = false , ?IncomingRequest $ request = null )
142166 {
143167 $ request ??= Services::request ();
144- $ path = $ request ->getPath ();
168+ /** @var CLIRequest|IncomingRequest $request */
169+ $ routePath = $ request ->getPath ();
170+ $ currentURI = $ request ->getUri ();
145171
146172 // Append queries and fragments
147- if ($ query = $ request -> getUri () ->getQuery ()) {
148- $ path . = '? ' . $ query ;
173+ if ($ query = $ currentURI ->getQuery ()) {
174+ $ query = '? ' . $ query ;
149175 }
150- if ($ fragment = $ request -> getUri () ->getFragment ()) {
151- $ path . = '# ' . $ fragment ;
176+ if ($ fragment = $ currentURI ->getFragment ()) {
177+ $ fragment = '# ' . $ fragment ;
152178 }
153179
154- $ uri = _get_uri ($ path );
180+ $ uri = _get_uri ($ routePath . $ query . $ fragment );
155181
156182 return $ returnObject ? $ uri : URI ::createURIString ($ uri ->getScheme (), $ uri ->getAuthority (), $ uri ->getPath ());
157183 }
0 commit comments