Hatena::Groupcatalyst

LAPISLAZULI HILL#Catalyst このページをアンテナに追加 RSSフィード

2007-01-05

[][]Chain#04 Chain#04 - LAPISLAZULI HILL#Catalyst を含むブックマーク はてなブックマーク - Chain#04 - LAPISLAZULI HILL#Catalyst Chain#04 - LAPISLAZULI HILL#Catalyst のブックマークコメント

こんな感じでどうだろう?

を実現するのに

sub user : Chained('/') PathPart('') CaptureArgs(1) {
    my ($self, $c, $user) = @_;
    $c->stash->{ user } = $user;
}

sub list : Chained('user') PathPart('') Args(0) {
    my ($self, $c) = @_;
    $c->stash->{ message } = "'s list";
}

sub detail : Chained('user') PathPart('detail') Args(1) {
    my ($self, $c, $url) = @_;
    $c->stash->{ message } = " detail => " .$url;
}

sub add : Chained('user') PathPart('add') Args(1) {
    my ($self, $c, $url) = @_;
    $c->stash->{ message } .= " add url => " .$url;
}

sub edit : Chained('user') PathPart('edit') Args(1) {
    my ($self, $c, $url) = @_;
    $c->stash->{ message } .= " edit url => " .$url;
}

sub del : Chained('user') PathPart('del') Args(1) {
    my ($self, $c, $url) = @_;
    $c->stash->{ message } .= " delete url => " .$url;
}

sub end : ActionClass('RenderView') {
    my ($self, $c) = @_;
    $c->response->body( $c->stash->{ user }.$c->stash->{ message });
}

下の4つは同じ

最後の(url)を/が入っても全部取るならArgsの数の指定をやめればすべて取ってくれる

sub add : Chained('user') PathPart('add') Args {
    my ($self, $c, @url) = @_;
    $c->stash->{ message } .= " add url => " .join "/", @url;
}
[debug] Loaded Chained actions:
.-------------------------------------+--------------------------------------.
| Path Spec                           | Private                              |
+-------------------------------------+--------------------------------------+
| /*/add/...                          | /user (1)                            |
|                                     | => /add                              |
| /*/del/...                          | /user (1)                            |
|                                     | => /del                              |
| /*/detail/...                       | /user (1)                            |
|                                     | => /detail                           |
| /*/edit/...                         | /user (1)                            |
|                                     | => /edit                             |
| /*                                  | /user (1)                            |
|                                     | => /list                             |
'-------------------------------------+--------------------------------------'

ふむ

[][]Chain#03 Chain#03 - LAPISLAZULI HILL#Catalyst を含むブックマーク はてなブックマーク - Chain#03 - LAPISLAZULI HILL#Catalyst Chain#03 - LAPISLAZULI HILL#Catalyst のブックマークコメント

Catalyst in Chains#02 - LAPISLAZULI HILL#Catalyst - Catalystグループの役に立たないメモの続き

Chained('/')

明示的に書いたほうがいいかも

sub language : Chained('/') PathPart('') CaptureArgs(1) {
    my ($self, $c, $language) = @_;
    $c->stash->{language} = $language;
}

それはともかく自分で読み返してみて,やっぱり全くわかってないことがわかったのでちゃんとドキュメントとか読む

マニュアルの(hallo/*/world/*)

http://search.cpan.org/dist/Catalyst-Runtime/lib/Catalyst/DispatchType/Chained.pm

sub hello : Chained('/') PathPart('hello') CaptureArgs(1) {
    my ($self, $c, $int) = @_;
    $c->stash->{ message } = "Hello ";
    $c->stash->{ arg_sum } = $int;
}

sub world : Chained('hello') PathPart('world') Args(1) {
    my ($self, $c, $int) = @_;
    $c->stash->{ message } .= "World!";
    $c->stash->{ arg_sum } += $int;

    $c->response->body( join "<br/>\n",
                        $c->stash->{ message },
                        $c->stash->{ arg_sum },);
}
[debug] Loaded Chained actions:
.-------------------------------------+--------------------------------------.
| Path Spec                           | Private                              |
+-------------------------------------+--------------------------------------+
| /hello/*/world/*                    | /hello (1)                           |
|                                     | => /world                            |
'-------------------------------------+--------------------------------------'

http://localhost:3000/hello/12345/world/67890

Hello World!
80235

ふむ.引数ごとに処理をして動作ができる

処理を増やす(foo/*/bar/* x2)

sub math : Chained('/') PathPart('math') CaptureArgs(1) {
    my ($self, $c, $int) = @_;
    $c->stash->{ message } = "Math?: $int";
    $c->stash->{ arg_sum } = $int;
}

sub plus : Chained('math') PathPart('plus') Args(1) {
    my ($self, $c, $int) = @_;
    $c->stash->{ message } .= " + $int";
    $c->stash->{ arg_sum } += $int;
}

sub minus : Chained('math') PathPart('minus') Args(1) {
    my ($self, $c, $int) = @_;
    $c->stash->{ message } .= " - $int";
    $c->stash->{ arg_sum } -= $int;
}

sub end : ActionClass('RenderView') {
    my ($self, $c) = @_;
    $c->response->body( join "<br/>\n",
                        $c->stash->{ message },
                        $c->stash->{ arg_sum },);
}
[debug] Loaded Chained actions:
.-------------------------------------+--------------------------------------.
| Path Spec                           | Private                              |
+-------------------------------------+--------------------------------------+
| /math/*/minus/*                     | /math (1)                            |
|                                     | => /minus                            |
| /math/*/plus/*                      | /math (1)                            |
|                                     | => /plus                             |
'-------------------------------------+--------------------------------------'

増えてる

http://colinux:3000/math/12345/plus/67890

Math?: 12345 + 67890
80235

http://colinux:3000/math/12345/minus/67890

Math?: 12345 - 67890
-55545

ああ,これはathPart('')にしたらさいしょのといっしょだなぁ


最後の引数を指定しないのも取る(foo/*/bar, foo/*/bar/*)

別の例にしたほうがいいかも知れないけど強引に

sub math : Chained('/') PathPart('math') CaptureArgs(1) {
    my ($self, $c, $int) = @_;
    $c->stash->{ message } = "Math?: $int";
    $c->stash->{ arg_sum } = $int;
}

sub plus : Chained('math') PathPart('plus') Args(1) {
    my ($self, $c, $int) = @_;
    $c->stash->{ message } .= " + $int";
    $c->stash->{ arg_sum } += $int;
}

sub plus_zero : Chained('math') PathPart('plus') Args(0) {
    my ($self, $c, $int) = @_;
    $c->stash->{ message } .= " + 0";
}

sub end : ActionClass('RenderView') {
    my ($self, $c) = @_;
    $c->response->body( join "<br/>\n",
                        $c->stash->{ message },
                        $c->stash->{ arg_sum },);
}
[debug] Loaded Chained actions:
.-------------------------------------+--------------------------------------.
| Path Spec                           | Private                              |
+-------------------------------------+--------------------------------------+
| /math/*/plus/*                      | /math (1)                            |
|                                     | => /plus                             |
| /math/*/plus                        | /math (1)                            |
|                                     | => /plus_zero                        |
'-------------------------------------+--------------------------------------'

ひとつ目はさっきといっしょ

http://colinux:3000/math/12345/plus

Math?: 12345 + 0
12345

さいしょのに加えて引数がない場合は別の動作ができると

[][]Catalyst in Chains#02 Catalyst in Chains#02 - LAPISLAZULI HILL#Catalyst を含むブックマーク はてなブックマーク - Catalyst in Chains#02 - LAPISLAZULI HILL#Catalyst Catalyst in Chains#02 - LAPISLAZULI HILL#Catalyst のブックマークコメント

Chain#01 - LAPISLAZULI HILL#Catalyst - Catalystグループの役に立たないメモの続き

Chainedをふたつ(NG)

ちょっと強引だけどこんなのはどうだ

sub fromlanguage : Chained PathPart('') CaptureArgs(1) {
    my ($self, $c, $fromlanguage) = @_;
    $c->stash->{fromlanguage}   = $fromlanguage;
}

sub language : Chained PathPart('fromlanguage') CaptureArgs(1) {
    my ($self, $c, $language) = @_;
    $c->stash->{language}   = $language;
}

sub show_hoge : Chained('fromlanguage') Chained('language') PathPart('hoge') Args(1) {
    my ($self, $c, $args) = @_;
    $c->response->body( $c->stash->{fromlanguage} ." from " .$c->stash->{language} .": hoge : $args");
}
[debug] Loaded Chained actions:
.-------------------------------------+--------------------------------------.
| Path Spec                           | Private                              |
+-------------------------------------+--------------------------------------+
| /*/hoge/*                           | /fromlanguage (1)                    |
|                                     | => /show_hoge                        |
'-------------------------------------+--------------------------------------'

動くけど思ったのとは違う

あえてこんなのは?

sub fromlanguage : Chained PathPart('') CaptureArgs(1) {
    my ($self, $c, $fromlanguage) = @_;
    $c->stash->{fromlanguage}   = $fromlanguage;
}

sub language : Chained PathPart('fromlanguage') CaptureArgs(1) {
    my ($self, $c, $language) = @_;
    $c->stash->{language}   = $language;
}

sub show_hoge : Chained('fromlanguage/language') PathPart('hoge') Args(1) {
    my ($self, $c, $args) = @_;
    $c->response->body( $c->stash->{fromlanguage} ." from " .$c->stash->{language} .": hoge : $args");
}
[debug] Loaded Chained actions:
.-------------------------------------+--------------------------------------.
| Path Spec                           | Private                              |
+-------------------------------------+--------------------------------------+
'-------------------------------------+--------------------------------------'

***空っぽだ!***

CaptureArgsを2にしてみた(*/*/hoge/*)

sub language : Chained PathPart('') CaptureArgs(2) {
    my ($self, $c, $language, $tolanguage) = @_;
    $c->stash->{language}   = $language;
    $c->stash->{tolanguage} = $tolanguage;
}

sub show_hoge : Chained('language') PathPart('hoge') Args(1) {
    my ($self, $c, $args) = @_;
    $c->response->body( $c->stash->{language} . " to " . $c->stash->{tolanguage} .": hoge : $args");
}
[debug] Loaded Chained actions:
.-------------------------------------+--------------------------------------.
| Path Spec                           | Private                              |
+-------------------------------------+--------------------------------------+
| /*/*/hoge/*                         | /language (2)                        |
|                                     | => /show_hoge                        |
'-------------------------------------+--------------------------------------'

http://localhost:3000/jp/en/hoge/12345

jp to en: hoge : 12345

これがほしかった動作だ!

languageでふたつの引数を取ることができた

PathPartをふたつ(NG)

sub language : Chained PathPart('') CaptureArgs(1) {
    my ($self, $c, $language) = @_;
    $c->stash->{language}   = $language;
}

sub edit_hoge : Chained('language') PathPart('hoge') Args(1) PathPart('edit'){
    my ($self, $c, $args) = @_;
    $c->response->body( $c->stash->{language}  .": hoge : $args : edit");
}
Multiple PathPart attributes not supported registering edit_hoge at /home/lapis25/perl/lib/Catalyst/Dispatcher.pm line 409
Compilation failed in require at script/myapp_server.pl line 53.

これはエラーになる.確かに意味は無いな.Argsを2にするんだろう


[][]flavour flavour - LAPISLAZULI HILL#Catalyst を含むブックマーク はてなブックマーク - flavour - LAPISLAZULI HILL#Catalyst flavour - LAPISLAZULI HILL#Catalyst のブックマークコメント

Catalyst Advent Calendar - Day 20を試して見た.やったのは去年だけど

あっさり動きました.コードは記事そのままなので省略.flavour(読み方はフレーバ)とはBlosxomにおいて拡張子によるテンプレートの切り替えを行う仕組みのようです.Blosxomは使ったことがないので適当なこと言ってるかもしれません

これをどう使うかな?

ddeqcaddeqca2007/04/11 12:47<a href=http://roccofoloper.sblog.cz>sauna belt</a> sauna belt http://roccofoloper.sblog.cz sauna belt

トラックバック - http://catalyst.g.hatena.ne.jp/lapis25/20070105