Skip to content

Core\Router\Responder

The interface your class implements to write the response from the value that a route method returns.

Core\Router\Responder extends Core\Router\Gate, so your class has the three Gate methods and one more: respond($route, $value). When a route method returns a value, Core\Router::dispatch calls respond with that value. Your respond method writes the response, for example with Core\Response::sendHtml or Core\Response::sendJson.

A route method declared void does not return a value, so dispatch does not call respond. A method declared ?T that returns null still reaches respond with null.

Good to know: allows still runs first. A refused request and a route method that throws an error never reach respond. If respond throws an error, dispatch throws the same error. nvs check gives a warning when your gate is not a Responder and a route method returns a value.

Examples

A page returns its markup

<?nvs
// Shows a route method that returns its page, and a gate that sends the page.
use Core\Http\Method;
class Blog
{
// This method returns the page. It does not send it.
#[Core\Route(path: '/post', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function post(): Core\Html\Markup
{
string $title = 'Tea & cake';
return html`<h1>{$title}</h1>`;
}
}
class Gate implements Core\Router\Responder
{
public function allows(Core\Router\Match $route): bool
{
return true;
}
public function refused(Core\Router\Refusal $reason, ?Core\Router\Match $route): void
{
echo 'you cannot open this page';
}
public function failed(?Core\Router\Match $route, ?Throwable $error): void
{
echo 'sorry, something went wrong';
}
// `dispatch` calls this with the page that the route method returned.
public function respond(Core\Router\Match $route, mixed $value): void
{
if ($value is Core\Html\Markup) {
Core\Response::sendHtml($value);
}
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
Core\Router::dispatch(new Gate());
} else {
// This prints `200`, the content type of the page, and the page.
// The `&` in the title is escaped.
var $post = Core\Test::request(Method::Get, '/post');
echo $post->status(), ' ', $post->header('Content-Type'), "\n";
echo $post->body(), "\n";
}
Output
200 text/html; charset=utf-8
<h1>Tea &amp; cake</h1>

An api method returns an object

<?nvs
// Shows an API route method that returns an object, and a gate that sends it as JSON.
use Core\Http\Method;
// `#[Core\Json\Derive]` lets `Core\Response::sendJson` write this class as JSON.
#[Core\Json\Derive]
class Product
{
public string $name;
public int $priceCents;
public function constructor(string $name, int $priceCents)
{
$this->name = $name;
$this->priceCents = $priceCents;
}
}
class Shop
{
// This method returns the product. It does not send it.
#[Core\Route(path: '/api/product', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function product(): Product
{
return new Product('Teapot', 2500);
}
}
class Gate implements Core\Router\Responder
{
public function allows(Core\Router\Match $route): bool
{
return true;
}
public function refused(Core\Router\Refusal $reason, ?Core\Router\Match $route): void
{
Core\Response::sendJson({ error: 'not allowed' });
}
public function failed(?Core\Router\Match $route, ?Throwable $error): void
{
Core\Response::sendJson({ error: 'server error' });
}
// `dispatch` calls this with the object that the route method returned.
public function respond(Core\Router\Match $route, mixed $value): void
{
Core\Response::sendJson($value);
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
Core\Router::dispatch(new Gate());
} else {
// This prints `200`, the content type, and the product as JSON.
var $product = Core\Test::request(Method::Get, '/api/product');
echo $product->status(), ' ', $product->header('Content-Type'), "\n";
echo $product->body(), "\n";
}
Output
200 application/json
{"name":"Teapot","priceCents":2500}

One respond for pages and data

<?nvs
// Shows one `respond` method that sends pages as HTML and every other value as JSON.
use Core\Http\Method;
class Shop
{
// This method returns a page.
#[Core\Route(path: '/', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function home(): Core\Html\Markup
{
return html`<h1>Welcome to the shop</h1>`;
}
// This method returns data: a list of product names.
#[Core\Route(path: '/api/products', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function products(): array<string>
{
return ['teapot', 'cup'];
}
// This method is `void`, so it writes its own response.
// `respond` does not run for it.
#[Core\Route(path: '/health', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function health(): void
{
echo 'ok';
}
}
class Gate implements Core\Router\Responder
{
public function allows(Core\Router\Match $route): bool
{
return true;
}
public function refused(Core\Router\Refusal $reason, ?Core\Router\Match $route): void
{
echo 'you cannot open this page';
}
public function failed(?Core\Router\Match $route, ?Throwable $error): void
{
echo 'sorry, something went wrong';
}
// A page is sent as HTML. Any other value is sent as JSON.
public function respond(Core\Router\Match $route, mixed $value): void
{
if ($value is Core\Html\Markup) {
Core\Response::sendHtml($value);
} else {
Core\Response::sendJson($value);
}
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
Core\Router::dispatch(new Gate());
} else {
// This prints the content type and the body of each response:
// first the page, then the JSON, then `ok`.
array<string> $paths = ['/', '/api/products', '/health'];
foreach ($paths as string $path) {
var $answer = Core\Test::request(Method::Get, $path);
echo $answer->header('Content-Type'), ' ', $answer->body(), "\n";
}
}
Output
text/html; charset=utf-8 <h1>Welcome to the shop</h1>
application/json ["teapot","cup"]
text/html; charset=utf-8 ok