NAME

Kelp - A web framework light, yet rich in nutrients.

SYNOPSIS

    package MyApp;
    use parent 'Kelp';

    # bootstrap your application
    sub build {
        my ($self) = @_;

        my $r = $self->routes;

        $r->add('/simple/route', 'route_handler');
        $r->add('/route/:name', {
            to => 'namespace::controller::action',
            ... # other options, see Kelp::Routes
        });
    }

    # example route handler
    sub route_handler {
        my ($kelp_instance, @route_parameters) = @_;

        return 'text to be rendered';
    }

    1;

DESCRIPTION

Kelp is a light, modular web framework built on top of Plack.

This document lists all the methods and attributes available in the main instance of a Kelp application, passed as a first argument to route handling routines.

See Kelp::Manual for a complete reference.

See Kelp::Manual::Cookbook for solutions to common problems.

ATTRIBUTES

hostname

Gets the current hostname.

    sub some_route {
        my $self = shift;
        if ( $self->hostname eq 'prod-host' ) {
            ...
        }
    }

mode

Sets or gets the current mode. The mode is important for the app to know what configuration file to merge into the main configuration. See Kelp::Module::Config for more information.

    my $app = MyApp->new( mode => 'development' );
    # conf/config.pl and conf/development.pl are merged with priority
    # given to the second one.

request_obj

Provide a custom package name to define the global ::Request object. Defaults to Kelp::Request.

response_obj

Provide a custom package name to define the global ::Response object. Defaults to Kelp::Response.

config_module

Sets of gets the class of the configuration module to be loaded on startup. The default value is Config, which will cause the Kelp::Module::Config to get loaded. See the documentation for Kelp::Module::Config for more information and for an example of how to create and use other config modules.

loaded_modules

A hashref containing the names and instances of all loaded modules. For example, if you have these two modules loaded: Template and JSON, then a dump of the loaded_modules hash will look like this:

    {
        Template => Kelp::Module::Template=HASH(0x208f6e8),
        JSON     => Kelp::Module::JSON=HASH(0x209d454)
    }

This can come in handy if your module does more than just registering a new method into the application. Then, you can use its object instance to access that additional functionality.

path

Gets the current path of the application. That would be the path to app.psgi

name

Gets or sets the name of the application. If not set, the name of the main class will be used.

    my $app = MyApp->new( name => 'Twittar' );

charset

Sets of gets the encoding charset of the app. It will be UTF-8, if not set to anything else. The charset could also be changed in the config files.

long_error

When a route dies, Kelp will by default display a short error message. Set this attribute to a true value if you need to see a full stack trace of the error. The KELP_LONG_ERROR environment variable can also set this attribute.

req

This attribute only makes sense if called within a route definition. It will contain a reference to the current Kelp::Request instance.

    sub some_route {
        my $self = shift;
        if ( $self->req->is_json ) {
            ...
        }
    }

res

This attribute only makes sense if called within a route definition. It will contain a reference to the current Kelp::Response instance.

    sub some_route {
        my $self = shift;
        $self->res->json->render( { success => 1 } );
    }

METHODS

new

    my $the_only_kelp = KelpApp->new;

A standard constructor. Cannot be called multiple times: see "new_anon".

new_anon

    my $kelp1 = KelpApp->new_anon(config => 'conf1');
    my $kelp2 = KelpApp->new_anon(config => 'conf2');

A constructor that can be called repeatedly. Cannot be mixed with "new".

It works by creating a new anonymous class extending the class of your application and running new on it. ref $kelp will return something else than the name of your Kelp class, but $kelp->isa('KelpApp') will be true. This will likely be useful during testing or when running multiple instances of the same application with different configurations.

build

On its own, the build method doesn't do anything. It is called by the constructor, so it can be overridden to add route destinations and initializations.

    package MyApp;

    sub build {
        my $self = shift;
        my $r = $self->routes;

        # Load some modules
        $self->load_module("MongoDB");
        $self->load_module("Validate");

        # Add all route destinations
        $r->add("/one", "one");
        ...

    }

load_module

load_module($name, %options)

Used to load a module. All modules must be under the Kelp::Module:: namespace.

    $self->load_module("Redis", server => '127.0.0.1');
    # Will look for and load Kelp::Module::Redis

Options for the module may be specified after its name, or in the modules_init hash in the config. Precedence is given to the inline options. See Kelp::Module for more information on making and using modules.

build_request

This method is used to create the request object for each HTTP request. It returns an instance of the class defined in the request_obj attribute (defaults to Kelp::Request), initialized with the current request's environment. You can override this method to use a custom request module if you need to do something interesting. Though there is a provided attribute that can be used to overide the class of the object used.

    package MyApp;
    use MyApp::Request;

    sub build_request {
        my ( $self, $env ) = @_;
        return MyApp::Request->new( app => $app, env => $env );
    }

    # Now each request will be handled by MyApp::Request

before_finalize

Override this method to modify the response object just before it gets finalized.

    package MyApp;

    sub before_finalize {
        my $self = shift;
        $self->res->set_header("X-App-Name", "MyApp");
    }

    ...

The above is an example of how to insert a custom header into the response of every route.

build_response

This method creates the response object, e.g. what an HTTP request will return. By default the object created is Kelp::Response though this can be overwritten via the respone_obj attribute. Much like "build_request", the response can also be overridden to use a custom response object if you need something completely custom.

run

This method builds and returns the PSGI app. You can override it in order to include middleware. See "Adding middleware" in Kelp::Manual for an example.

param

A shortcut to $self->req->param:

    sub some_route {
        my $self = shift;
        if ( $self->param('age') > 18 ) {
            $self->can_watch_south_path(1);
        }
    }

This function can be tricky to use because of context sensivity. See "param" in Kelp::Request for more information and examples.

session

A shortcut to $self->req->session. Take a look at "session" in Kelp::Request for more information and examples.

stash

Provides safe access to $self->req->stash. When called without arguments, it will return the stash hash. If called with a single argument, it will return the value of the corresponding key in the stash. See "stash" in Kelp::Request for more information and examples.

named

Provides safe access to $self->req->named. When called without arguments, it will return the named hash. If called with a single argument, it will return the value of the corresponding key in the named hash. See "named" in Kelp::Request for more information and examples.

url_for

A safe shortcut to $self->routes->url. Builds a URL from path and arguments.

    sub build {
        my $self = shift;
        $self->routes->add("/:name/:id", { name => 'name', to => sub {
            ...
        }});
    }

    sub check {
        my $self = shift;
        my $url_for_name = $self->url_for('name', name => 'jake', id => 1003);
        $self->res->redirect_to( $url_for_name );
    }

abs_url

Same as "url_for", but returns the full absolute URI for the current application (based on configuration).

AUTHOR

Stefan Geneshky - minimal <at> cpan.org

LICENSE

This module and all the modules in this package are governed by the same license as Perl itself.