Skip to content

xaicron/Kagura

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NAME

Kagura - simple web application framework

SYNOPSIS

Generating new project:

$ kagura-setup.pl MyApp

run app.psgi

$ cd MyApp
$ plackup MyApp.psgi

DESCRIPTION

Kagura is easy, simple, lightweight web application framework.

EXPORT FUNCTIONS

get($path, $code)
post($path, $code)
any([$method, ] $path, $code)

This functions from Router::Simple::Sinatraish.

$code must be returned Plack::Response-like object.

package MyApp;
use MyApp::Web;

get '/' => sub {
    my ($c, $req) = @_;
    ...
    my $res = $c->response_class->new(200, [], ['Hello Kagura!!']);
    return $res;
};
post '/' => sub {
    my ($c, $req) = @_;
    ...
    return $res;
};
any '/' => sub {
    my ($c, $req) = @_;
    ...
    return $res;
};
any [qw/GET DELETE/], '/any' => sub {
    my ($c, $req) = @_;
    ...
};

# $c   is Kagura-like object
# $req is Plack::Requst-like object (eq $c->req)
# $res is Plack::Response-like object
dispatch($contoroller, $method_name)

Sets dispatch rule.

into MyApp.pm

package MyApp;
use MyApp::Web;

# call MyApp::Web::C::Root::index
get '/' => dispatch('Root', 'index');

into MyApp/Web/C/Root.pm

package MyApp::Web::C::Root;

sub index {
    my ($c, $req) = @_;
    ...
}

METHOD

req()

Get request object.

get '/' => sub {
   my ($c, $req) = @_;
   $c->req; # eq $req
   ...
};
params()

Route parameters.

get '/{user}' => sub {
   my ($c, $req) = @_;
   my $params = $c->params;
   ...
};

# GET http://localhost/foo
# $params->{user} eq 'foo'
render(@args)

Rendering template. Returned response_class object.

get '/' => sub {
   my ($c, $req) = @_;
   ...
   my $res = $c->render('index.mt');
   return $res;
};
model($name)

Get MyApp::M::$name object.

package MyApp::M::User;
sub find {
    my ($self, $user_id) = @_;
    ...
    return $user_name;
}

package MyApp;
use parent 'MyApp::Web';
get '/{user_id}' => sub {
    my ($c, $req) = @_;
    my $user_name = $c->model('User')->find($c->params->{user_id});
    ...
};
return_404()

Returned status 404.

get '/404' => sub {
   my ($c, $req) = @_;
   return $c->return_404();
};
return_403()

Returned status 403.

get '/403' => sub {
   my ($c, $req) = @_;
   return $c->return_403();
};
show_error()

Returned status 500.

get '/error' => sub {
   my ($c, $req) = @_;
   return $c->show_error('oops!!');
};
stash([$hashref])

You can use freely.

CLASS METHOD

to_app()

into app.psgi

use MyApp;
MyApp->to_app;
init()

Initialize application. This method in to_app() called.

init_prepare()

Call prepare init() method. This method is in inii() called.

You can override this method.

package MyApp::Web;
use parent 'Kagura';

sub init_prepare {
    my ($class) = @_;
    ...
}
init_finalize()

Call finalize init() method. This method is in inii() called.

You can override this method.

package MyApp::Web;
use parent 'Kagura';

sub init_finalize {
    my ($class) = @_;
    ...
}
load_plugin($class [, $config])

Load plugin class. this method must be calling in init_finalzie() or after init().

package MyApp::Web;
use parent 'Kagura'

sub init_finalize {
    my ($class) = @_;
    $class->load_plugin('Web::JSON');
    $class->load_plugin('+MyApp::Plugin::Foo', +{ bar => 'baz' });
}
load_plugins(%args)

Load plugins.

package MyApp::Web;
use parent 'Kagura';

sub init_finalize {
    my ($class) = @_;
    $class->load_plugins(
      'Web::JSON'           => {},
      '+MyApp::Plugin::Foo' => +{ bar => 'baz' }
    );
}

or configuration:

# conf/developlemt.pl
+{
    plugin => +{
        'Web::JSON'           => +{},
        '+MyApp::Plugin::Foo' => +{ bar => 'baz' },
    },
}

DEFAULT CLASS ACCESSOR METHOD

These methods you can calling the after init().

config([$hash_ref])

Loaded configuration from conf/$ENV{PLACK_ENV}.pl

home_dir([$home_dir])

Sets value must be Path::Class-like object.

renderer([$renderer])

Sets value must be Tiffany::* object.

container([$scalar])

Default 'Object::Container'

request_class([$scalar])

Default 'Plack::Request'

response_class([$scalar])

Default 'Plack::Response'

AUTHOR

xaicron <xaicron {at} cpan.org>

COPYRIGHT

Copyright 2011 - xaicron

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

About

minimalistic web application framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages