Skip to content

Commit

Permalink
Merge branch 'master' of github.com:heimrichhannot/contao-utils-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Wagner committed Feb 15, 2018
2 parents 259353f + c22c76d commit f58877c
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/Url/UrlUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function getJumpToPageObject(int $jumpTo, bool $fallbackToObjPage = true)
protected function prepareUrl($url)
{
if (null === $url) {
$url = Environment::get('request');
$url = Environment::get('requestUri');
} elseif (is_numeric($url)) {
if (null === ($jumpTo = PageModel::findByPk($url))) {
throw new \InvalidArgumentException('Given page id does not exist.');
Expand All @@ -163,4 +163,60 @@ protected function prepareUrl($url)

return $url;
}

/**
* Redirect to another page
*
* @param string $strLocation The target URL
* @param integer $intStatus The HTTP status code (defaults to 303)
*/
public function redirect($strLocation, $intStatus=303)
{
if (headers_sent())
{
exit;
}

$strLocation = str_replace('&', '&', $strLocation);

// Make the location an absolute URL
if (!preg_match('@^https?://@i', $strLocation))
{
$strLocation = \Environment::get('base') . ltrim($strLocation, '/');
}

// Ajax request
if (\Environment::get('isAjaxRequest'))
{
header('HTTP/1.1 204 No Content');
header('X-Ajax-Location: ' . $strLocation);
}
else
{
// Add the HTTP header
switch ($intStatus)
{
case 301:
header('HTTP/1.1 301 Moved Permanently');
break;

case 302:
header('HTTP/1.1 302 Found');
break;

case 303:
header('HTTP/1.1 303 See Other');
break;

case 307:
header('HTTP/1.1 307 Temporary Redirect');
break;
}

header('Location: ' . $strLocation);
}

exit;
}
}

0 comments on commit f58877c

Please sign in to comment.