アプリケーション作成の流れ最初にあるウェブサイトで、会員情報を扱う画面(群)を作る事にする。このサイトのアプリケーション名はmyappとする。 データベースを作るデータベースmyappdbを作り、そこにmemberテーブルを作る。 モデルを作るスキーマクラスを作る
package MyappDB;
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_classes(
{
MyappDB => [qw/Member/],
}
);
1;
テーブルクラスを作る
package MyappDB::Member;
use strict;
use warnings;
use base 'DBIx::Class';
__PACKAGE__->load_components(qw/ PK::Auto Core/);
__PACKAGE__->table('member');
__PACKAGE__->add_columns(qw/
member_id
name
age
/);
__PACKAGE__->set_primary_key('login_id');
1;
モデルクラスを作る以下のようにヘルパースクリプトを実行する。 script/myapp_create.pl model MyappDB DBIC::Schema MyappDB
'dbi:Pg:dbname=myapp_db;host=localhost' 'pgsql' 'passwd'
すると、lib/Myapp/Model/MyappDB.pmというモデルクラスができる。
コントローラを作るscript/myapp_create.pl controller Member MemberはURLの一部になる。(例:http://myapp/member/indexなど) ビューを作るscript/myapp_create.pl view TT TTSite
処理(ロジック)を書くメイン処理コントローラにあたるlib/Myapp/Controller/Member.pm を開き、以下のようにコントローラアクションを書く。 sub list : Local {
my ( $self, $c ) = @_;
$c->stash->{members} = [$c->model('MyappDB::Member')->all];
$c->stash->{template} = 'member/list.tt2';
}
テンプレート表示用処理以下のようなendアクションを書き、自動的にテンプレートを表示するようにする。 sub end : ActionClass('RenderView') {}
テンプレートファイルを書くlib/Myapp/Controller/Member.pm に以下のようにテンプレートファイルを書く。 <ul> [% FOREACH member IN members -%] <li>[% member.login_id %]</li> [% END -%] </ul> ミニサーバを起動し、動作を確認するscript/myapp_server.pl -r |
|