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->{key};
await $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
Methods
new
$object = $class->new(%args)
Standard Mooish constructor. Consult "Attributes" section for available constructor arguments.
stash
Delegated method for "stash" in PAGI::Request
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.
SEE ALSO
Thunderhorse, Gears::Context, Thunderhorse::Request, Thunderhorse::Response, Thunderhorse::WebSocket, Thunderhorse::SSE