-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRedirect.php
68 lines (64 loc) · 1.99 KB
/
Redirect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace Luracast\Restler;
use Luracast\Restler\Format\JsonFormat;
/**
* Static class for handling redirection
*
* @category Framework
* @package Restler
* @author R.Arul Kumaran <[email protected]>
* @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/
*
*/
class Redirect
{
/**
* Redirect to given url
*
* @param string $url relative path or full url
* @param array $params associative array of query parameters
* @param array $flashData associative array of properties to be set in $_SESSION for one time use
* @param int $status http status code to send the response with ideally 301 or 302
*
* @return array
*/
public static function to($url, array $params = array(), array $flashData = array(), $status = 302)
{
$url = ltrim($url, '/');
/** @var $r Restler */
$r = Scope::get('Restler');
$base = $r->getBaseUrl() . '/';
if (0 !== strpos($url, 'http')) {
$url = $base . $url;
}
if (!empty($flashData) || $base . $r->url !== $url || Util::getRequestMethod() != 'GET') {
if ($r->responseFormat instanceof JsonFormat) {
return array('redirect' => $url);
}
if (!empty($params)) {
$url .= '?' . http_build_query($params);
}
Flash::set($flashData);
header(
"{$_SERVER['SERVER_PROTOCOL']} $status " .
(isset(RestException::$codes[$status]) ? RestException::$codes[$status] : '')
);
header("Location: $url");
die('');
}
return array();
}
/**
* Redirect back to the previous page
*
* Makes use of http referrer for redirection
*
* @return array
*/
public static function back()
{
return static::to($_SERVER['HTTP_REFERER']);
}
}