NAME
Thunderhorse::Context - Request handling context
SYNOPSIS
async sub show ($self, $ctx, $id)
{
my $query_param = $ctx->req->query('name');
my $stashed_value = $ctx->stash->get('key');
my $session_value = $ctx->session->get('key');
$ctx->res->text("Hello World");
}
DESCRIPTION
Thunderhorse::Context represents the context of a single HTTP request. It extends Gears::Context and provides access to the request, response, WebSocket connection, and Server-Sent Events stream. Each context manages the lifecycle of request processing.
The context object is passed to route handlers and provides the primary interface for interacting with the HTTP request and generating responses.
Since this object is created for every request, costly type checks are disabled conditionally with the help of Devel::StrictMode. Refer to its documentation to learn how they can be enabled on demand.
INTERFACE
Inherits all interface from Gears::Context, and adds the interface documented below.
Attributes
pagi
A tuple [HashRef, CodeRef, CodeRef] containing the PAGI scope hash, receiver, and sender.
Required in the constructor
match
The router match object (Gears::Router::Match) or array ref containing match information for the current route.
writer: set_match
req
The Thunderhorse::Request object for this context. Created automatically with a reference to this context.
res
The Thunderhorse::Response object for this context. Created automatically with a reference to this context.
ws
The Thunderhorse::WebSocket object for this context. Created lazily when first accessed and will throw an exception if the PAGI scope is not a WebSocket scope.
predicate: has_ws
sse
The Thunderhorse::SSE object for this context. Created lazily when first accessed and will throw an exception if the PAGI scope is not a Server-Sent Events scope.
predicate: has_sse
stash
The PAGI::Stash object for this context. Created lazily when first accessed.
session
The PAGI::Session object for this context. Created lazily when first accessed.
Note that for session to work properly, PAGI::Middleware::Session must be used, or other middleware that will populate pagi.session scope key. PAGI::Session will throw an exception if pagi.session is not present in scope.
connection
Connection object for this context, taken from PAGI scope. Connection objects are inserted into the scope by the server, and there is no clear subclass they will inherit from. They follow a duck-typed interface though, and can be trusted to have at least response_started method.
Methods
new
$object = $class->new(%args)
Standard Mooish constructor. Consult "Attributes" section for available constructor arguments.
scope
$scope = $ctx->scope()
Returns the PAGI scope hash (the first element of the PAGI tuple).
receiver
$receiver = $ctx->receiver()
Returns the PAGI receiver callback (the second element of the PAGI tuple).
sender
$sender = $ctx->sender()
Returns the PAGI sender callback (the third element of the PAGI tuple).
update
$ctx->update($scope, $receive, $send)
Updates PAGI tuple elements in the context and in all subobjects (request, response, sse, websocket). This is done automatically before a route handler is called.
consume
$ctx->consume()
Marks the context as consumed, preventing further processing. Returns the context object for chaining.
is_consumed
$bool = $ctx->is_consumed()
Returns true if the context has been consumed either explicitly via "consume", or implicitly by sending a response, closing a WebSocket connection, or closing an SSE stream.
empty_res
$new_res = $ctx->empty_res()
Discards the current response attached to the context, then builds and returns a fresh one. Useful if you want to completely discard response data.
send_res
await $ctx->send_res()
Tries to send the response back to the client. Dies if the response has already been sent. Force-sends the response even if it has an empty body.
try_send_res
$ctx->try_send_res()
Similar as "send_res", but does nothing if the response has already been sent. Also skips sending the response if it is not ready yet, according to "is_ready" in Thunderhorse::Response.
Note that you don't have to use this method explicitly. Thunderhorse will automatically use it after the route handler returns.
SEE ALSO
Thunderhorse, Gears::Context, Thunderhorse::Request, Thunderhorse::Response, Thunderhorse::WebSocket, Thunderhorse::SSE