diff --git a/helpers/url.php b/helpers/url.php index e4a417b1..57bacc40 100644 --- a/helpers/url.php +++ b/helpers/url.php @@ -34,7 +34,7 @@ function secure_site_url(string $sUrl = null): string } if (!function_exists('redirect')) { - function redirect(string $sUrl = null, string $sMethod = 'location', int $iHttpResponseCode = 302): void + function redirect(string $sUrl = null, string $sMethod = 'location', ?int $iHttpResponseCode = null): void { Url::redirect($sUrl, $sMethod, $iHttpResponseCode); } diff --git a/src/Common/Helper/Url.php b/src/Common/Helper/Url.php index 8515d247..3faddbb7 100644 --- a/src/Common/Helper/Url.php +++ b/src/Common/Helper/Url.php @@ -16,6 +16,7 @@ use Nails\Common\Exception\FactoryException; use Nails\Common\Exception\NailsException; use Nails\Common\Service\FileCache; +use Nails\Config; use Nails\Factory; use Pdp; use Psr\SimpleCache\InvalidArgumentException; @@ -29,6 +30,8 @@ class Url { const PUBLIC_SUFFIX_LIST = 'https://publicsuffix.org/list/public_suffix_list.dat'; + const REDIRECT_HEADER_CODE_DEFAULT = 302; + // -------------------------------------------------------------------------- /** @@ -67,7 +70,7 @@ public static function siteUrl(string $sUrl = null, bool $bForceSecure = false): * @throws FactoryException * @throws NailsException */ - public static function redirect(string $sUrl = null, string $sMethod = 'location', int $iHttpResponseCode = 302): void + public static function redirect(string $sUrl = null, string $sMethod = 'location', ?int $iHttpResponseCode = null): void { /** * Call the Bootstrap::shutdown method, the system will be killed in approximately 13 @@ -87,6 +90,12 @@ public static function redirect(string $sUrl = null, string $sMethod = 'location $sUrl = siteUrl($sUrl); } + // -------------------------------------------------------------------------- + + $iHttpResponseCode = $iHttpResponseCode ?? Config::get('NAILS_REDIRECT_HEADER_CODE_DEFAULT', static::REDIRECT_HEADER_CODE_DEFAULT); + + // -------------------------------------------------------------------------- + switch ($sMethod) { case 'refresh': header('Refresh:0;url=' . $sUrl);