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
<?phpusesa\application\controller;usesa\application\responses\View;classUserControllerextendscontroller {publicfunctionindex() {// Use master theme.// Render the '/{module}/views/user_list.php' view $html =newView('master','user_list');// Pass variables $first_name and $last_name into HTML view. $html->data =array('first_name'=>'John','last_name'=>'Doe');// Render the viewreturn $html; }}
All responses must implement the sa\application\responses\ISaResponse interface. This requires your custom response to include the following methods,
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().
Custom Response Example
<?phpusesa\application\responses\ISaResponse;usesa\application\responses\TViews;usesa\application\responses\ResponseHeader;classYamlimplementsISaResponse {useTViews;public $data =array();private $responseCode;publicfunction__construct($responseCode =200) {$this->responseCode = $responseCode; }/** * Gets called before getHeaders and getResponse */publicfunctionbuildResponse() {$this->setResponseCode($this->responseCode);$this->headers[] =newResponseHeader('Content-Type','application/x-yaml'); }/** * Gets the response content to be echo to the user * * @returnstring */publicfunctiongetResponse() {returnyaml_emit($this->data); }/** * Gets the headers to be sent to the user * * @returnarray(); */publicfunctiongetHeaders() {return$this->headers; }}
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:
The layout to wrap around the HTML content. Use null for no layout. Use master for default application layout.
$view
string
required
The HTML content to render in the template. Siteadmin will search for the template in /themes/{theme}/views, then /modules/{module}/views if not found.
$location
string
optional
Location of the view. To load a view from a different module, use \{vendor}\{module}\SomeController::viewLocation().