Quantcast
Channel: 詩と創作・思索のひろば
Viewing all articles
Browse latest Browse all 179

Router::Simple::Reversible という Perl モジュールを書いた

$
0
0

https://metacpan.org/pod/Router::Simple::Reversible

だいぶ昔に書いていたのを shipit しました。tokuhiromさんの Router::Simpleに、いわゆるリバースルーティングを足しただけのモジュール。Router::Simpleを継承しているので、blessしなおすなりして使えばよいかと思います。

新しく生えるメソッド $router->path_for$router->matchで返ってくるような hashref を渡すと、いい感じにルーティングのルールが具体化されたかたちで文字列が返ってくるシンプルな API です。テストを見るとだいたい使い方がわかります:

my$router = Router::Simple::Reversible->new;

# from Router::Simple's pod$router->connect('/', {controller => 'Root', action => 'show'});
$router->connect('/blog/{year}/{month}', {controller => 'Blog', action => 'monthly'});
$router->connect('/wiki/:page', { controller => 'WikiPage', action => 'show' } );
$router->connect('/download/*.*', { controller => 'Download', action => 'file' } );

is $router->path_for({ controller => 'Root', action => 'show' }),
   '/';

is $router->path_for({ controller => 'Blog', action => 'monthly' }, { year => 2015, month => 10 }),
   '/blog/2015/10';

is $router->path_for({ controller => 'WikiPage', action => 'show' }, { page => 'HelloWorld' }),
   '/wiki/HelloWorld';

is $router->path_for({ controller => 'Download', action => 'file' }, { splat => [ 'path/to/file', 'xml' ] }),
   '/download/path/to/file.xml';

is $router->path_for({ controller => 'NoSuchController', action => 'show' }),
   undef;

何か変なところがあったら GitHub リポジトリのほうまでお知らせください。


Viewing all articles
Browse latest Browse all 179

Trending Articles