Router::Simple悩み中
※追記あり
以下のようにRouter::SimpleをつかうとUserのRequestから実行すべきController、actionを判定できます。
かつ、ユーザーがどの年月を指定しているかといった情報も知る事ができます。
use strict; use warnings; use Router::Simple; use Data::Dumper; my $router = Router::Simple->new(); $router->connect( '/blog/{year}/{month}', { controller => 'Blog', action => 'monthly' } ); my $env = { 'PATH_INFO' => '/blog/2011/05', 'REQUEST_METHOD' => 'GET', }; my $match = $router->match($env); print Dumper $match; =result $VAR1 = { 'controller' => 'Blog', 'month' => '05', 'action' => 'monthly', 'year' => '2011' }; =cut 1;
なのですが、自分で書いたWeb::Controllerは次のようにcapturesをハッシュではなく、配列で渡したい。
My::Web::Controller::Blog->monthly($c, @captures);
package My::Web::Controller::Blog; sub monthly { my ( $class, $c, $year, $month ) = @_; ... }
つまりRouter::Simple::Route->match($env)にはこうした情報を返してほしい
$VAR1 = { 'controller' => 'Blog', 'month' => '05', # 不要 'action' => 'monthly', 'year' => '2011' # 不要 'captures' => ['2010', '05'], };
Router::Simple::Route->matchが返すハッシュリファレンスにcapturesを加えると最も簡単。
以下'0.09'のRouter::Simple::Routeの108行目から下の行に次のような処理を加えたい。
if ( @captured ) { $match->{captures} = [@captured]; }
ただし、そうすると元々あったテストが次々とこける。
t/01_simple.t .. not ok 1 # Failed test at t/01_simple.t line 15. # Structures begin differing at: # $got->{captures} = ARRAY(0x1008317a8) # $expected->{captures} = Does not exist
うーん。元々のテストに影響を与えずにcaputresを配列で取得したい場合はどうすれば良いだろう。
ついでにcapturesを配列で受け取る場合はこれらをhash化する必要はないので記述を省略したい。
追記(2011-05-22 20:00:00)解決しました。
splatというものを使うと配列で記憶します。以下のように書き換えます。
$router->connect( '/blog/*/*', { controller => 'Blog', action => 'monthly' } );
$VAR1 = { 'controller' => 'Blog', 'splat' => [ '2011', '05' ], 'action' => 'monthly' };
08_splat.tに書いてあったorz