Basics Output

Of course, all routes and controllers should return some kind of response to be sent back to the user's browser.

A instance of the current HTTP reponse as a \wiggum\http\Reponse object is always passed into your Controller. This allows you to receive the response object that has already been passed through any middlewares you have setup.

<?php
namespace app\components\helloWorld;

use \wiggum\model\Component;
use \wiggum\model\Request;
use \wiggum\model\Response;

class HelloWorld extends Controller 
{
	// ...
	
	/**
     *
     * @param  Request  $request
     * @param  Response  $response
     * @return Response
     */
	public function doDefault(Request $request, Response $response) 
	{
		$response->setOutput('Hello World!');
		
		return $response;
	}
	
	// ...
}
					
					

The given string will automatically be converted into an HTTP response by the framework.

Every Controller method must return the response object.

Other Response Types

Below is an example of setting the content type to application/json and passing a json object as the output.

$response->setContentType('application/json');
$response->setOutput(json_encode($output, JSON_NUMERIC_CHECK));

Attaching Headers

You may use the addHeader method to add a series of headers to the response before sending it back to the user:

$response->addHeader('Content-Type', $type);
$response->addHeader('X-Header-One', 'Header Value');
$response->addHeader('X-Header-Two', 'Header Value');

Or, you may use the setHeaders method to specify an array of headers to be added to the response:

$response->setHeaders([
	'Content-Type' => $type,
	'X-Header-One' => 'Header Value',
	'X-Header-Two' => 'Header Value',
]);

Redirects

Defining a redirect will set all the proper headers needed to redirect the user to another URL.

$response->setRedirect('/home/');

Methods

Method Parameters Description
getContentType -
setContentType $contentType
setHeaders $headers
getHeaders -
addHeader $name
$value
getHeader $name
setOutput $output
appendOutput $output
getOutput -
setRedirect $redirect
getRedirect -
withStatus $code
$reasonPhrase
getReasonPhrase -
getStatusCode -