28 public static function header($headerField, $value,
bool $replace =
true,
int $responseCode = 0): void {
29 self::
$headers [trim ( $headerField )] = trim ( $value );
30 Startup::getHttpInstance ()->header ( trim ( $headerField ), trim ( $value ), $replace, $responseCode );
38 public static function forward(
string $url,
bool $preserveUrl=
true): void {
39 self::header(
'Location',$preserveUrl?$url:
URequest::getUrl($url));
47 private static function _headerArray($headerField, $values): void {
48 if (\is_array ( $values )) {
49 $values = \implode (
', ', $values );
51 self::header ( $headerField, $values );
60 public static function setContentType($contentType, $encoding =
null): void {
61 $value = $contentType;
62 if (isset ( $encoding ))
63 $value .=
'; charset=' . $encoding;
64 self::header (
'Content-Type', $value,
true );
71 self::header (
'Cache-Control',
'no-cache, must-revalidate' );
72 self::header (
'Expires',
'Sat, 26 Jul 1997 05:00:00 GMT' );
82 public static function isSent(&$file =
null, &$line =
null): bool {
83 return
Startup::getHttpInstance ()->headersSent ( $file, $line );
89 public static function asJSON(): void {
90 self::header (
'Content-Type',
'application/json' );
99 public static function isJSON(): bool {
100 return isset ( self::$headers [
'Content-Type'] ) && self::$headers [
'Content-Type'] ===
'application/json';
108 public static function asHtml($encoding =
'utf-8'): void {
109 self::setContentType (
'text/html', $encoding );
117 public static function asXml($encoding =
'utf-8'): void {
118 self::setContentType (
'application/xml', $encoding );
126 public static function asText($encoding =
'utf-8'): void {
127 self::setContentType (
'plain/text', $encoding );
137 self::header (
'Accept', $value );
148 public static function enableCors($origin =
'*', $methods =
'GET, POST, PUT, DELETE, PATCH, OPTIONS', $headers =
'X-Requested-With, Content-Type, Accept, Origin, Authorization'): void {
149 self::setAccessControlOrigin ( $origin );
150 self::setAccessControlMethods ( $methods );
151 self::setAccessControlHeaders ( $headers );
161 self::header (
'Access-Control-Allow-Origin', $origin );
162 if ($origin !==
'*') {
163 self::header (
'Vary',
'Origin' );
173 self::_headerArray (
'Access-Control-Allow-Methods', $methods );
182 self::_headerArray (
'Access-Control-Allow-Headers', $headers );
191 self::header (
'Authorization', $authorization );
200 return \http_response_code ( $value );
209 return \http_response_code ();
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
static isJSON()
Tests if response content-type is application/json Only Works if UResponse has been used for setting ...
static asHtml($encoding='utf-8')
Sets the response content-type to text/html.
static enableCors($origin=' *', $methods='GET, POST, PUT, DELETE, PATCH, OPTIONS', $headers='X-Requested-With, Content-Type, Accept, Origin, Authorization')
Enables CORS.
static forward(string $url, bool $preserveUrl=true)
Forwards to url using Location header.
static isSent(&$file=null, &$line=null)
Checks if or where headers have been sent.
static setAccessControlHeaders($headers)
Sets the Access-Control-Allow-Headers field value.
static setAccessControlOrigin($origin=' *')
Sets the Access-Control-Allow-Origin field value Only a single origin can be specified.
static asText($encoding='utf-8')
Sets the response content-type to plain/text.
static getResponseCode()
Get the response code.
static setAccept($value)
Sets the Accept header.
static header($headerField, $value, bool $replace=true, int $responseCode=0)
Send a raw HTTP header.
static setAccessControlMethods($methods)
Sets the Access-Control-Allow-Methods field value.
static asXml($encoding='utf-8')
Sets the response content-type to application/xml.
static setAuthorization($authorization)
Set the Authorization header field.
static setContentType($contentType, $encoding=null)
Sets header content-type.
static noCache()
Forces the disabling of the browser cache.
static _headerArray($headerField, $values)
static setResponseCode($value)
Sets the response code.
static asJSON()
Sets the response content-type to application/json.