Kagura - simple web application framework
Generating new project:
$ kagura-setup.pl MyApprun app.psgi
$ cd MyApp
$ plackup MyApp.psgiKagura is easy, simple, lightweight web application framework.
- get($path, $code)
- post($path, $code)
- any([$method, ] $path, $code)
-
This functions from Router::Simple::Sinatraish.
$codemust 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) = @_; ... }
- 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_classobject.get '/' => sub { my ($c, $req) = @_; ... my $res = $c->render('index.mt'); return $res; }; - model($name)
-
Get
MyApp::M::$nameobject.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.
- 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' }, }, }
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'
xaicron <xaicron {at} cpan.org>
Copyright 2011 - xaicron
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.