NAME

Pono::Context - The object passed to Pono handlers

SYNOPSIS

$app->get('/hello/:name' => sub {
    my ($c) = @_;
    my $name = $c->req->param('name');
    $c->header('X-Powered-By' => 'Pono');
    return $c->json({ hello => $name });
});

DESCRIPTION

A context is created for each request and passed to handlers and middleware as $c. It holds the request and builds the response.

METHODS

req

Returns the Pono::Request for this request.

env

Returns the PSGI environment.

text, html, json

$c->text('Hello');
$c->html('<h1>Hello</h1>');
$c->json({ hello => 'world' });
$c->text('Created', 201, { 'X-Id' => 42 });

Build a response with the matching Content-Type. The status and extra headers (a hash or array reference) are optional. Strings are encoded as UTF-8.

If you set a Content-Type yourself, with header or in the extra headers, it is used instead of the default one.

body

$c->body($bytes, 200, { 'Content-Type' => 'image/png' });

Builds a response without a Content-Type.

redirect

$c->redirect('/login');
$c->redirect('/new', 301);

Builds a redirect. The status defaults to 302.

not_found

Builds the response of the handler set with $app->not_found, or a plain 404 Not Found.

$c->header('Cache-Control' => 'no-store');

Adds a header to the response. Before the response is built, the header is kept for it. After it is built (in middleware, after $next->()), the header is added to it. Returns $c.

status

$c->status(201);

Sets the default status for the response that is built later. Returns $c.

set, get

$c->set(user => $user);
my $user = $c->get('user');

Keep a value for the length of the request, to pass it from middleware to handlers.

res

my $res = $c->res;
$c->res([ 200, [], ['replaced'] ]);

Reads or replaces the response built so far, as a PSGI response. Useful in middleware after $next->().

SEE ALSO

Pono

LICENSE

Copyright (C) Yusuke Wada.

This library is free software; you can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE file in this distribution.