Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class Utils
public static function apiUrl(string $path, ?array $queryParams = [], int $port = 80): string
{
$isLocalDevelopment = app()->environment(['local', 'development']);
$baseURL = url($path, $queryParams, !$isLocalDevelopment);
// Laravel's url() renders extra parameters as path segments, not a query string,
// so the query string must be built here
$baseURL = url($path, [], !$isLocalDevelopment);

// Check if default port is used to avoid appending it unnecessarily
if (!in_array($port, [80, 443])) {
Expand All @@ -52,6 +54,10 @@ public static function apiUrl(string $path, ?array $queryParams = [], int $port
}
}

if (!empty($queryParams)) {
$baseURL .= (str_contains($baseURL, '?') ? '&' : '?') . http_build_query($queryParams);
}

return $baseURL;
}

Expand Down
6 changes: 5 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,15 @@ function storage_path(string $path = ''): string
if (!function_exists('url')) {
function url(string $path = '', mixed $parameters = [], ?bool $secure = null): string
{
// Mirrors Illuminate\Routing\UrlGenerator::to(): extra parameters become
// rawurlencoded path segments with keys discarded, NOT a query string
$base = $secure ? 'https://fleetbase.test' : 'http://fleetbase.test';
$path = '/' . ltrim($path, '/');

if (is_array($parameters) && $parameters !== []) {
return $base . $path . '?' . http_build_query($parameters);
$tail = implode('/', array_map('rawurlencode', array_values($parameters)));

return $base . rtrim($path, '/') . '/' . $tail;
}

return $base . $path;
Expand Down
Loading