Documentation

HttpResponse
in package

FinalYes

HTTP response builder utility class.

Provides fluent interface for building HTTP responses including JSON, HTML, JavaScript, plain text, file downloads, images, and error responses. Supports CORS headers and internal redirects for Nginx X-Accel-Redirect.

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 all response data.
download()  : void
Send file download response.
error()  : void
Send error response.
html()  : void
Send HTML response.
image()  : void
Send image response with appropriate Content-Type.
internalRedirect()  : void
Internal redirect.
js()  : void
Send JavaScript response.
json()  : void
Send JSON response.
set()  : HttpResponse
Set response data.
setCorsHeader()  : void
Set CORS (Cross-Origin Resource Sharing) headers.
status()  : HttpResponse
Set the HTTP response status code.
text()  : void
Send plain text response.
view()  : void
Render a Twig template and send as HTML response.

Properties

$data

Response data.

private array<string|int, mixed> $data = []

Methods

__construct()

Initialize HttpResponse.

public __construct() : mixed

download()

Send file download response.

public download(string $filename[, string $content = '' ][, bool $mime = false ]) : void
Parameters
$filename : string

Download filename for the browser.

$content : string = ''

File content to download.

$mime : bool = false

MIME type. False for auto-detection.

error()

Send error response.

public error(string $message[, int $httpStatus = 500 ][, bool $forceJsonResponse = false ]) : void

For AJAX requests or when forced, responds with JSON. Otherwise, uses CodeIgniter's show_error() for HTML error pages.

Parameters
$message : string

Error message.

$httpStatus : int = 500

HTTP status code. Default is 500.

$forceJsonResponse : bool = false

Force JSON response format. Default is false.

Tags
throws
LogicException

If JSON encoding fails.

html()

Send HTML response.

public html(string $html) : void
Parameters
$html : string

HTML content string.

image()

Send image response with appropriate Content-Type.

public image(string $imagePath) : void
Parameters
$imagePath : string

Path to the image file.

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()

Send JavaScript response.

public js(string $js) : void
Parameters
$js : string

JavaScript code.

json()

Send JSON response.

public json([bool $forceObject = false ][, bool $prettyrint = false ]) : void

Outputs response data as JSON with Content-Type application/json.

Parameters
$forceObject : bool = false

Force object output for non-associative arrays.

$prettyrint : bool = false

Pretty-print JSON with indentation.

Tags
throws
LogicException

If JSON encoding fails.

set()

Set response data.

public set(mixed $key[, mixed|null $value = null ]) : HttpResponse

With one argument, replaces all response data. With two arguments, sets a single field.

Parameters
$key : mixed

Response data (1 arg) or field name (2 args).

$value : mixed|null = null

Field value when using 2 arguments.

Return values
HttpResponse

Method chaining.

setCorsHeader()

Set CORS (Cross-Origin Resource Sharing) headers.

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 the HTTP response status code.

public status(int $httpStatus) : HttpResponse
Parameters
$httpStatus : int

HTTP status code (e.g., 200, 404, 500).

Return values
HttpResponse

Method chaining.

text()

Send plain text response.

public text(string $plainText) : void
Parameters
$plainText : string

Plain text content.

view()

Render a Twig template and send as HTML response.

public view(string $templatePath) : void
Parameters
$templatePath : string

Template path relative to views directory.


        
On this page

Search results