Files
dolibarr/htdocs/includes/restler/Redirect.php
jfefe b503b16f07 NEW : add restler framework
First step to build REST API into Dolibarr
2015-05-01 15:42:05 +02:00

53 lines
1.7 KiB
PHP

<?php
namespace Luracast\Restler;
use Luracast\Restler\Format\JsonFormat;
/**
* Static class for handling redirection
*
* @category Framework
* @package Restler
* @author R.Arul Kumaran <arul@luracast.com>
* @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/
* @version 3.0.0rc5
*/
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();
}
}