簡単なアプリケーション
とりあえずモデル使わずに、
- アプリケーションクラス
- ベースコントローラ
- ベースビュー
- コントローラ2つ
- ビュー2つ
を用意してみました。
package Hello;
use strict;
use warnings;
use Catalyst qw/-Debug Dumper Static::Simple/;
our $VERSION = '0.01';
__PACKAGE__->config( name => 'Hello' );
__PACKAGE__->setup;
sub default : Private {
my ( $self, $c ) = @_;
}
sub end : Private {
my ( $self, $c ) = @_;
$c->stash->{'body'} = $c->welcome_message() unless (length($c->stash->{'body'}) > 0);
$c->log->debug($c->stash->{'body'});
$c->response->header('Content-Type' => 'text/html; charset=euc-jp');
$c->response->body(sprintf(qq!<html><body>%s</body></html>!, $c->stash->{'body'}));
return $c->response->body;
}
1;package Hello::BaseController;
use strict;
use warnings;
use base 'Catalyst::Controller';
# 使用するビューを返す
sub get_view : Private {
die "must implements at child class";
}
# どのアクションにも一致しなかったら呼ばれる
sub default : Private {
my ( $self, $c ) = @_;
$c->forward($self->get_view(), 'default');
}
# 追加アクション
sub add : Local {
my ( $self, $c ) = @_;
$c->forward($self->get_view(), 'add');
}
# 無効化アクション
sub disable : Local {
my ( $self, $c ) = @_;
$c->forward($self->get_view(), 'disable');
}
# 削除アクション
sub delete : Local {
my ( $self, $c ) = @_;
$c->forward($self->get_view(), 'delete');
}
# 詳細表示アクション
sub view : Local {
my ( $self, $c ) = @_;
$c->forward($self->get_view(), 'view');
}
# 編集アクション
sub edit : Local {
my ( $self, $c ) = @_;
$c->forward($self->get_view(), 'edit');
}
# 一覧アクション
sub list : Local {
my ( $self, $c ) = @_;
$c->forward($self->get_view(), 'list');
}
1;package Hello::BaseView;
use strict;
use warnings;
use base 'Catalyst::View';
sub default {
my ( $self, $c ) = @_;
$c->stash->{body} = ref($self) . ":default<br>\n";
}
sub add {
my ( $self, $c ) = @_;
$c->stash->{body} = ref($self) . ":add<br>\n";
my @args = @{$c->req->args};
foreach my $arg (@args) {
$c->stash->{body} .= sprintf(qq!arg: %s<br>\n!, $arg);
}
}
sub disable {
my ( $self, $c ) = @_;
$c->stash->{body} = ref($self) . ":disable<br>\n";
my @args = @{$c->req->args};
foreach my $arg (@args) {
$c->stash->{body} .= sprintf(qq!arg: %s<br>\n!, $arg);
}
}
sub delete {
my ( $self, $c ) = @_;
$c->stash->{body} = ref($self) . ":delete<br>\n";
my @args = @{$c->req->args};
foreach my $arg (@args) {
$c->stash->{body} .= sprintf(qq!arg: %s<br>\n!, $arg);
}
}
sub view {
my ( $self, $c ) = @_;
$c->stash->{body} = ref($self) . ":view<br>\n";
my @args = @{$c->req->args};
foreach my $arg (@args) {
$c->stash->{body} .= sprintf(qq!arg: %s<br>\n!, $arg);
}
}
sub edit {
my ( $self, $c ) = @_;
$c->stash->{body} = ref($self) . ":edit<br>\n";
my @args = @{$c->req->args};
foreach my $arg (@args) {
$c->stash->{body} .= sprintf(qq!arg: %s<br>\n!, $arg);
}
}
sub list {
my ( $self, $c ) = @_;
$c->stash->{body} = ref($self) . ":list<br>\n";
my @args = @{$c->req->args};
foreach my $arg (@args) {
$c->stash->{body} .= sprintf(qq!arg: %s<br>\n!, $arg);
}
}
1;package Hello::Controller::Form1;
use strict;
use warnings;
use base 'Hello::BaseController';
# 使用するビューを返す
sub get_view : Private {
my ( $self, $c ) = @_;
return "Hello::View::Form1";
}
1;package Hello::Controller::Form2;
use strict;
use warnings;
use base 'Hello::BaseController';
# 使用するビューを返す
sub get_view : Private {
my ( $self, $c ) = @_;
return "Hello::View::Form2";
}
1;package Hello::View::Form1; use strict; use warnings; use base 'Hello::BaseView'; # 各メソッドをオーバーライドして使います 1;
package Hello::View::Form2; use strict; use warnings; use base 'Hello::BaseView'; # 各メソッドをオーバーライドして使います 1;