Controller
extends CI_Controller
in package
Base controller class extending CI_Controller.
Provides enhanced response handling, auto-loading of models/libraries, CORS support, and hook points for response processing.
Usage:
class UserController extends \X\Controller\Controller {
protected $model = ['UserModel', 'RoleModel'];
protected $library = 'session';
public function index() {
$this->set('users', $this->UserModel->findAll())->view('user/index');
}
}
Table of Contents
Properties
- $httpResponse : HttpResponse
- HTTP response handler instance.
- $library : string|array<string|int, string>
- Library(s) to auto-load on instantiation.
- $model : string|array<string|int, string>
- Model(s) to auto-load on instantiation.
Methods
- __construct() : mixed
- Initialize controller and auto-load models/libraries.
- internalRedirect() : void
- Perform internal redirect (X-Accel-Redirect).
- beforeDownload() : void
- Hook called before file download response.
- beforeInternalRedirect() : void
- Hook called before internal redirect.
- beforeResponse() : void
- Hook called before any response. Override to add global response processing.
- beforeResponseHtml() : void
- Hook called before HTML response.
- beforeResponseImage() : void
- Hook called before image response.
- beforeResponseJs() : void
- Hook called before JavaScript response.
- beforeResponseJson() : void
- Hook called before JSON response.
- beforeResponseText() : void
- Hook called before plain text response.
- beforeResponseView() : void
- Hook called before template view response.
- clear() : Controller
- 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.
- js() : void
- Send JavaScript response.
- json() : void
- Send JSON response.
- set() : Controller
- Set response data.
- setCorsHeader() : Controller
- Set CORS (Cross-Origin Resource Sharing) headers.
- status() : Controller
- Set HTTP response status code.
- text() : void
- Send plain text response.
- view() : void
- Render and send Twig template response.
- getReferer() : string
- Get HTTP referrer URL.
Properties
$httpResponse
HTTP response handler instance.
protected
HttpResponse
$httpResponse
$library
Library(s) to auto-load on instantiation.
protected
string|array<string|int, string>
$library
Single library name or array of library names.
$model
Model(s) to auto-load on instantiation.
protected
string|array<string|int, string>
$model
Single model name or array of model names.
Methods
__construct()
Initialize controller and auto-load models/libraries.
public
__construct() : mixed
internalRedirect()
Perform internal redirect (X-Accel-Redirect).
public
internalRedirect(string $redirectPath) : void
Allows internal redirection to a location determined by a header returned from the backend. This enables the backend to authenticate and perform processing, then serve content from an internally redirected location while freeing the backend for other requests.
Parameters
- $redirectPath : string
-
Internal redirect path.
Tags
beforeDownload()
Hook called before file download response.
protected
beforeDownload(string $referer) : void
Parameters
- $referer : string
-
Referrer URL.
Tags
beforeInternalRedirect()
Hook called before internal redirect.
protected
beforeInternalRedirect(string $referer) : void
Parameters
- $referer : string
-
Referrer URL.
Tags
beforeResponse()
Hook called before any response. Override to add global response processing.
protected
beforeResponse(string $referer) : void
Parameters
- $referer : string
-
Referrer URL.
Tags
beforeResponseHtml()
Hook called before HTML response.
protected
beforeResponseHtml(string $referer) : void
Parameters
- $referer : string
-
Referrer URL.
Tags
beforeResponseImage()
Hook called before image response.
protected
beforeResponseImage(string $referer) : void
Parameters
- $referer : string
-
Referrer URL.
Tags
beforeResponseJs()
Hook called before JavaScript response.
protected
beforeResponseJs(string $referer) : void
Parameters
- $referer : string
-
Referrer URL.
Tags
beforeResponseJson()
Hook called before JSON response.
protected
beforeResponseJson(string $referer) : void
Parameters
- $referer : string
-
Referrer URL.
Tags
beforeResponseText()
Hook called before plain text response.
protected
beforeResponseText(string $referer) : void
Parameters
- $referer : string
-
Referrer URL.
Tags
beforeResponseView()
Hook called before template view response.
protected
beforeResponseView(string $referer) : void
Parameters
- $referer : string
-
Referrer URL.
Tags
clear()
Clear all response data.
protected
clear() : Controller
Return values
Controller —Method chaining.
download()
Send file download response.
protected
download(string $filename[, string $content = '' ][, bool $mime = false ]) : void
Parameters
- $filename : string
-
Download filename for the browser.
- $content : string = ''
-
File content or path to file.
- $mime : bool = false
-
MIME type. False for auto-detection.
Tags
error()
Send error response.
protected
error(string $message[, int $httpStatus = 500 ][, bool $forceJsonResponse = false ]) : void
Outputs an error message with the specified HTTP status code.
Parameters
- $message : string
-
Error message to display.
- $httpStatus : int = 500
-
HTTP status code. Default is 500.
- $forceJsonResponse : bool = false
-
Force JSON response format. Default is false.
Tags
html()
Send HTML response.
protected
html(string $html) : void
Parameters
- $html : string
-
HTML content string.
image()
Send image response.
protected
image(string $imagePath) : void
Outputs an image file with appropriate Content-Type header.
Parameters
- $imagePath : string
-
Path to the image file.
Tags
js()
Send JavaScript response.
protected
js(string $js) : void
Parameters
- $js : string
-
JavaScript code.
json()
Send JSON response.
protected
json([bool $forceObject = false ][, bool $prettyrint = false ]) : void
Parameters
- $forceObject : bool = false
-
Force object output for non-associative arrays.
- $prettyrint : bool = false
-
Pretty-print JSON with whitespace.
Tags
set()
Set response data.
protected
set(mixed $key[, mixed|null $value = null ]) : Controller
Parameters
- $key : mixed
-
Response data (1 arg) or field name (2 args).
- $value : mixed|null = null
-
Field value when using 2 arguments.
Tags
Return values
Controller —Method chaining.
setCorsHeader()
Set CORS (Cross-Origin Resource Sharing) headers.
protected
setCorsHeader([string $origin = '*' ]) : Controller
Parameters
- $origin : string = '*'
-
Allowed origin(s). Use '*' for all or space-separated URLs.
Tags
Return values
Controller —Method chaining.
status()
Set HTTP response status code.
protected
status(int $httpStatus) : Controller
Parameters
- $httpStatus : int
-
HTTP status code (e.g., 200, 404, 500).
Return values
Controller —Method chaining.
text()
Send plain text response.
protected
text(string $plainText) : void
Parameters
- $plainText : string
-
Plain text content.
view()
Render and send Twig template response.
protected
view(string $templatePath) : void
Parameters
- $templatePath : string
-
Path to Twig template file.
Tags
getReferer()
Get HTTP referrer URL.
private
getReferer() : string
Returns the HTTP_REFERER if available, otherwise constructs the current URL.
Return values
string —Referrer URL.