ResponseInterface - viames/pair GitHub Wiki

Pair framework: ResponseInterface

Pair\Http\ResponseInterface is the explicit response contract handled by the Pair v4 request flow.

Any controller action can return an object implementing this interface. Pair then calls send() and skips the legacy implicit view path.

Built-in implementations include:

Contract

namespace Pair\Http;

/**
 * Explicit response contract handled by the v4 request flow.
 */
interface ResponseInterface {

	/**
	 * Send the response to the current output channel.
	 */
	public function send(): void;

}

Action example

use Pair\Http\JsonResponse;
use Pair\Http\ResponseInterface;

/**
 * Return an explicit JSON response from an API action.
 */
public function healthAction(): ResponseInterface {

	return new JsonResponse(['ok' => true]);

}

Custom response example

Use a custom implementation only when the built-in response objects do not fit.

use Pair\Http\ResponseInterface;

/**
 * Stream a small CSV response without entering the legacy view path.
 */
final readonly class CsvResponse implements ResponseInterface {

	/**
	 * Build the CSV response from already-escaped rows.
	 *
	 * @param	string[]	$rows	CSV lines without trailing newlines.
	 */
	public function __construct(private array $rows) {}

	/**
	 * Send the CSV headers and body.
	 */
	public function send(): void {

		header('Content-Type: text/csv; charset=utf-8', true);
		http_response_code(200);
		print implode("\n", $this->rows);

	}

}

Then return it from a controller:

/**
 * Export the current order list as CSV.
 */
public function exportAction(): ResponseInterface {

	return new CsvResponse([
		'id,number,status',
		'7,ORD-0007,paid',
	]);

}

Runtime behavior

When an action returns ResponseInterface:

  • Pair calls send().
  • PageResponse is still wrapped by the outer template.
  • Non-page responses such as JsonResponse, TextResponse, and EmptyResponse bypass template wrapping.
  • The legacy renderView() path is used only when no explicit response is returned.

See also: Controller, Application, PageResponse, JsonResponse, TextResponse, EmptyResponse.