HttpResponse
in package
HTTP response processing Utility.
Table of Contents
Properties
- $CI : CI_Controller
- CI_Controller instance.
- $data : array<string|int, mixed>
- Response data.
- $httpStatus : int
- HTTP Status.
Methods
- __construct() : mixed
- Initialize HttpResponse.
- clear() : HttpResponse
- Clear response.
- download() : void
- Download file.
- error() : void
- Error response.
- html() : void
- Response HTML.
- image() : void
- Response image.
- internalRedirect() : void
- Internal redirect.
- js() : void
- Response js.
- json() : void
- Response JSON.
- set() : HttpResponse
- Set response.
- setCorsHeader() : void
- Sets the CORS header.
- status() : HttpResponse
- Set HTTP status.
- text() : void
- Response Plain text.
- view() : void
- Responds with the result of compiling the specified template into HTML.
Properties
$CI
CI_Controller instance.
private
CI_Controller
$CI
$data
Response data.
private
array<string|int, mixed>
$data
= []
$httpStatus
HTTP Status.
private
int
$httpStatus
Methods
__construct()
Initialize HttpResponse.
public
__construct() : mixed
clear()
Clear response.
public
clear() : HttpResponse
Return values
HttpResponsedownload()
Download file.
public
download(string $filename[, string $content = '' ][, bool $mime = false ]) : void
Parameters
- $filename : string
-
Download file name.
- $content : string = ''
-
(optional) Downloadable Content.
- $mime : bool = false
-
(optional) MIME Type. The default is false and the MIME type is automatically detected.
error()
Error response.
public
error(string $message[, int $httpStatus = 500 ][, bool $forceJsonResponse = false ]) : void
Parameters
- $message : string
-
Error message.
- $httpStatus : int = 500
-
(optional) HTTP status.
- $forceJsonResponse : bool = false
-
(optional) Force a response with Content-Type "application/json".
html()
Response HTML.
public
html(string $html) : void
Parameters
- $html : string
-
HTML string.
image()
Response image.
public
image(string $imagePath) : void
Parameters
- $imagePath : string
-
Image path.
internalRedirect()
Internal redirect.
public
internalRedirect(string $redirectPath) : void
Allows for internal redirection to a location determined by a header returned from a backend. This allows the backend to authenticate and perform any other processing, provide content to the end user from the internally redirected location, and free up the backend to handle other requests.
Nginx:
# Will serve /var/www/files/myfile
# When passed URI /protected/myfile
location /protected {
internal;
alias /var/www/files;
}
``
PHP:
```php
class Sample extends \X\Controller\Controller {
public function index() {
parent::internalRedirect('/protected/myfile');
}
}
Parameters
- $redirectPath : string
-
Path to internal redirect.
js()
Response js.
public
js(string $js) : void
Parameters
- $js : string
-
JS Code.
json()
Response JSON.
public
json([bool $forceObject = false ][, bool $prettyrint = false ]) : void
Parameters
- $forceObject : bool = false
-
(optional) Outputs an object rather than an array when a non-associative array is used.
- $prettyrint : bool = false
-
(optional) Use whitespace in returned data to format it.
set()
Set response.
public
set(mixed $key[, mixed|null $value = null ]) : HttpResponse
Parameters
- $key : mixed
-
If one argument, the response data. If two arguments, the field name of the response data.
- $value : mixed|null = null
-
Response data.
Return values
HttpResponsesetCorsHeader()
Sets the CORS header.
public
setCorsHeader(string $origin) : void
// Allow all.
parent::setCorsHeader('*');
// Only any origin is allowed.
parent::setCorsHeader('http://www.example.jp');
parent::setCorsHeader('http://www.example.jp https://www.example.jp http://sub.example.jp');
// To set the same Access-Control-Allow-Origin for all responses, use the hook point called before the response.
abstract class AppController extends \X\Controller\Controller {
protected function beforeResponse(string $referer) {
$this->setCorsHeader('*');
}
}
Parameters
- $origin : string
-
Allowable Origins.
status()
Set HTTP status.
public
status(int $httpStatus) : HttpResponse
Parameters
- $httpStatus : int
-
HTTP status.
Return values
HttpResponsetext()
Response Plain text.
public
text(string $plainText) : void
Parameters
- $plainText : string
-
Plain text.
view()
Responds with the result of compiling the specified template into HTML.
public
view(string $templatePath) : void
Parameters
- $templatePath : string
-
Template path.