Responses
All controllers must return a response to be sent back to the user's browser. Siteadmin provides many response types which configure the appropriate headers for each content type.
HTML Response
Constructor Parameters
Field | Type | Optional/Required | Definition |
$template | string | required | The layout to wrap around the HTML content. Use |
$view | string | required | The HTML content to render in the template. Siteadmin will search for the template in |
$location | string | optional | Location of the view. To load a view from a different module, use |
$responseCode | int | optional | HTTP response code (200, 404, etc.) |
JSON Response
Constructor Parameters
Field | Type | Optional/Required | Definition |
$responseCode | int | optional | HTTP response code (200, 404, etc.) |
XML Response
Constructor Parameters
Field | Type | Optional/Required | Definition |
$rootTag | string | required | The XML document's root tag. |
$namespace | string | optional | XML namespace definition. |
$responseCode | int | optional | HTTP response code (200, 404, etc.) |
File/Binary Response
Renders a file, such as an image or PDF, in the browser.
Constructor Parameters
Field | Type | Optional/Required | Definition |
$filePath | string | required | Path to file. |
HTTP Redirect
Constructor Parameters
Field | Type | Optional/Required | Definition |
$location | string | required | A website URL. (e.g. http://domain.com or /path/on/current/website) |
$isPermanent | boolean | false | Default: false. If true, 301 redirect header will be included in the response. |
Custom Responses
The Response Interface
All responses must implement the sa\application\responses\ISaResponse
interface. This requires your custom response to include the following methods,
Method | Returns | Description |
buildResponse | void | Sets appropriate headers for the response. |
getResponse | string | Serializes the response's data into a string. |
getHeaders | array | Returns key-value pairs as an array, where the key is a header and the value is its value. |
The Response Trait
Most responses allow the developer to set custom headers and a custom response code. A wrapper for this exists through the \sa\application\responses\TViews
trait, which exposes a headers
property, as well as a helper method for setting the response code: setResponseCode()
.
Method/Property | Type | Description |
headers | array | An associative array of headers. The key is the header name. The value is the header content. |
setResponseCode($httpCode) | function | Sets the response code for the request to the given HTTP code. |
Custom Response Example
Downloading Files
Sometimes an application will need to download a file instead of rendering its contents in the browser. This is useful for exporting XML, images, word documents, zip files, among others.
The Downloadable Response Trait
Download capabilities are exposed through the DownloadableResponseTrait
. This trait exposes a setDownloadable()
method which accepts a file name as its only parameter. Once this method is executed, the response object will configure the necessary headers to download its contents as a file.
The following response types implement this trait:
JSON
XML
File
Download JSON
Download XML
Download Files
The File
response can be used to download any type of file. Common use cases include:
CSV
Images
PDFs
Word Documents
...or any other type of file.
Last updated