The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Kelp::Context - Tracks Kelp application's current execution context

SYNOPSIS

    # get current controller
    $app->context->current;

    # get the application
    $app->context->app;

    # get the named controller
    $app->context->controller('Controller');

DESCRIPTION

This is a small helper object which keeps track of the context in which the app currently is. It also remembers all the constructed controllers until it is cleared - which usually is at the start of the request.

Advanced usage only.

It can be subclassed to change how controllers are built and handled. This would usually involve overriding the build_controller method.

ATTRIBUTES

app

Main application object. This will always be the main app, not a controller.

current

Current controller object. This will be automatically set to a proper controller by the router.

req

res

Current request and response objects, also accessible from $app->req and $app->res.

persistent_controllers

A configuration field which defines whether "clear" destroys constructed controllers. By default it is taken from app's configuration field of the same name.

METHODS

build_controller

Defines how a controller is built. Can be overridden to introduce a custom controller object instead of reblessed application.

controller

Returns a controller of a given name. The name will be mangled according to the base route class of the application. Contains extra checks to ensure the input is valid and loads the controller class if it wasn't loaded yet.

If the controller name is undef, the base controller is returned.

set_controller

Similar to "controller", but does not have any special checks for correctness and only accepts a full class name. It also modifies the "current" to the controller after constructing it. Passing a false value will result in reverting the current context back to the app object.

It's optimized for speed and only used internally, so it's not recommended to use it unless you extend Kelp router itself.

clear

Clears context in anticipation of the next request. Called automatically at the start of every request.