Skip to content

Commit

Permalink
chore: Allow customisation of the default Redirect Header code
Browse files Browse the repository at this point in the history
  • Loading branch information
hellopablo committed Oct 28, 2024
1 parent 84eefed commit d426173
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion helpers/url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 10 additions & 1 deletion src/Common/Helper/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,8 @@ class Url
{
const PUBLIC_SUFFIX_LIST = 'https://publicsuffix.org/list/public_suffix_list.dat';

const REDIRECT_HEADER_CODE_DEFAULT = 302;

// --------------------------------------------------------------------------

/**
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit d426173

Please sign in to comment.