sub request_handler {
my ($req) = @_;
# ...
return $res;
}
MyApp.pm
package MyApp; use Ark; # additional config 1;
MyApp/Controller/Root.pm
package MyApp::Controller::Root;
use Ark 'Controller';
has '+namespace' => default => '';
sub index :Path {
my ($self, $c) = @_;
$c->res->body('Hello World!');
}
1;
BEGINとかしなくていい
package MyApp::Controller::Root;
use Moouse;
BEGIN { extends 'Catalyst::Controller' }
my $app = MyApp->new; $app->setup_minimal( action_cache => 'tmp/action.cache' );
ディスパッチテーブルキャッシュ
GET /
load MyApp::Controller::Root
$c->forward('/another_controller/action');
load MyApp::Controller::Another
$c->session
インクルードモデル
<?= include 'header' ?> ... <?= include 'footer' ?>
wrapperモデル
wrapper.tt
<html> <body> [% content %] </body> </html>
wrapperモデル
content.tt
<h1>Hello</h1>
継承モデル
base.mt
<html> <head> <title><?= block title => 'Default Title' ?></title> </head> <body> <?= block content => '' ?> </body> </html>
継承モデル
child.mt
? extends 'base'
? block content => sub {
<h1>Hello World</h1>
? } # endblock content
Perlで継承をサポートしたテンプレートエンジンは
Text::MicroTemplate::Extended と Template::Declare
だけ!(たぶん)
package MyApp::Model::Foo;
use Ark 'Model::Adaptor';
__PACKAGE__->config(
class => 'Foo',
);
$c->model('Foo');
モデル定義(オブジェクト登録)
package MyApp::Models;
use Ark::Models -Base;
register Foo => sub {
$_[0]->ensure_class_loaded('Foo');
Foo->new;
};
モデルの使用
use MyApp::Models;
my $foo = models->get('Foo');
my $foo = models('Foo');
モデルの使用
use MyApp::Models 'hoge';
my $foo = hoge->get('Foo');
my $foo = hoge('Foo');
CLIスクリプトでも
use MyApp::Models;
my $conf = models('conf');
Formモジュール(後述)から
custon_validation => sub {
my ($self, $form, $field) = @_;
my $value = $form->param($field->name);
my $user = models('Schema::User')->find({ username => $value })
if ($user) {
$form->set_error( $field->name, 'already_taken' );
}
},
register Schema => sub {
my $self = shift;
my $conf = $self->get('conf')->{database};
croak "Require database config" unless $conf and ref $conf eq 'ARRAY';
$self->ensure_class_loaded('MyApp::Schema');
MyApp::Schema->connect(@$conf);
};
register 'Schema::User' => sub {
my $self = shift;
my $schema = $self->get('Schema');
$schema->resultset('User');
};
models('Schema::User');
return {
database => [
'dbi:mysql:foo', 'root', undef,
{
on_connect_do => 'SET NAMES utf8',
mysql_enable_utf8 => 1,
},
],
};
my $home = MyApp::Models->get('home');
return {
my_file => $home->file('my_file'),
}
package MyApp; use Ark; use_model 'MyApp::Models'; 1;
package MyApp::Form::Signup;
use Ark 'Form';
param username => (
type => 'TextField',
label => 'Username',
constraints => [
'NOT_NULL',
'ASCII',
],
);
param password => (
type => 'PasswordField',
label => 'Password',
constraints => [
'NOT_NULL',
'ASCII',
],
);
1;
<?= form->render ?>
Or
<?= form->render('username') ?>
<?= form->render('password') ?>
package MyApp::Controller::Root;
use Ark 'Controller::Form';
sub signup :Local :Form('MyApp::Form::Signup') {
my ($self, $c) = @_;
if ($self->form->submitted_and_valid) {
# signup
}
}
package MyApp::Controller::Root; use Ark 'Controller'; with 'Ark::ActionClass::Form';
でも同じ
package MyApp::Form::Account;
use Ark 'Form';
param username => (
# ...
);
param password => (
# ...
);
1;
package MyApp::Form::Signup;
use Ark '+MyApp::Form::Account';
use MyApp::Models;
param '+username' => (
custon_validation => sub {
my ($self, $form, $field) = @_;
return if $form->is_error('username');
if (models('Schema::User')->find({ username => $form->param('username') })) {
$form->set_error( username => 'already_taken' );
}
},
);
sub messages {
return {
%{ shift->SUPER::messages },
'username.already_taken' => 'this username already taken',
};
}
だいたい一通りある
t/plugin_*
みると、なにがあるかと使い方がわかる
サポート。
ex: Ark::ActionClass::Form
use Ark 'Controller' use utf8;
use Ark 'Controller' no utf8;