NAME
KelpX::Controller - Base custom controller for Kelp
SYNOPSIS
# your base controller
package My::Controller;
use Kelp::Base 'KelpX::Controller';
sub build
{
# build your controller
}
# your application
package My::Kelp;
attr context_obj => 'KelpX::Controller::Context';
...
DESCRIPTION
Since Kelp 2.16 it's quite easy to introduce your own base controller class instead of subclassing the main application class. While Kelp gives you this option, it trusts you will develop your own infrastructure for that.
This module is a toolbox for less tedious integration of custom controller class into your application. It consists of two classes, KelpX::Controller
and KelpX::Controller::Context
. They must be used in tandem as shown in "SYNOPSIS".
The controller will be built just like a regular object the first time it's used. It will not be cleared after the request, since the context object will have its persistent_controllers
set to true by default. You may override "build" in the controller, but if you want to have it add any routes then you will have to instantiate it manually using $app->context->controller
.
ATTRIBUTES
context
Required. The app's context object.
app
The application object. Will be loaded from "context".
METHODS
build
A build method, which will be called right after the controller is instantiated. Takes no arguments and does nothing by default - it's up to you to override it.
To avoid a very common trap you may fall into if you set routes in your controller, you should put all route-setting code in your controller after a guard clause like the one below:
sub build
{
my ($self) = @_;
... # common code for controllers
return unless ref $self eq __PACKAGE__;
# all code below will not be repeated in subclasses
}
req
Proxy for reading the req
attribute from "context".
res
Proxy for reading the res
attribute from "context".
before_dispatch
Proxy for before_dispatch
from "app".
before_finalize
Proxy for before_finalize
from "app".
SEE ALSO
AUTHOR
Bartosz Jarzyna, <bbrtj.pro@gmail.com>
COPYRIGHT AND LICENSE
Copyright (C) 2024 by Bartosz Jarzyna
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.