3
Router::Simple::Cookbook - The Router::Simple Cookbook
7
=head2 How to create Sinatra-ish framework with Router::Simple?
9
Please read the following example code.
17
my $router = Router::Simple->new();
18
my $any = sub ($$;$) {
19
my ($pattern, $dest, $opt) = do {
21
my ($methods, $pattern, $code) = @_;
22
($pattern, {code => $code}, +{method => [ map { uc $_ } @$methods ]});
24
my ($pattern, $code) = @_;
25
($pattern, {code => $code}, +{});
35
# any [qw/get post delete/] => '/bye' => sub { ... };
36
# any '/bye' => sub { ... };
37
*{"${pkg}::any"} = $any;
38
*{"${pkg}::get"} = sub {
39
$any->([qw/GET HEAD/], $_[0], $_[1]);
41
*{"${pkg}::post"} = sub {
42
$any->([qw/POST/], $_[0], $_[1]);
44
*{"${pkg}::as_psgi_app"} = sub {
46
if (my $p = $router->match($_[0])) {
47
[200, [], [$p->{code}->()]];
49
[404, [], ['not found']];
65
=head2 How to switch from HTTPx::Dispatcher?
67
L<HTTPx::Dispatcher> is class specific declararative router.
69
package MyApp::Dispatcher;
71
connect '/', {controller => 'foo', action => 'bar'};
74
The following script is same as above.
76
package MyApp::Dispatcher;
77
use Router::Simple::Declare;
80
connect '/', {controller => 'foo', action => 'bar'};
82
sub match { $router->match() }
84
=head2 How to use Router::Simple with non-strictly-MVC application?
86
use Router::Simple::Declare;
88
connect '/foo/bar/' => { 'target' => '/foobar.asp' };
89
connect '/topics/:topic' => { target => '/my-topic.asp' };
90
connect '/products/{Category:.*}' => { target => '/products.asp', Category => 'All' };
91
connect '/zipcode/{zip:[0-9]{5,5}}' => {target => '/zipcode.asp' };
94
You can pass the target path as destination.
98
Tokuhiro Matsuno E<lt>tokuhirom AAJKLFJEF GMAIL COME<gt>
102
Copyright (C) Tokuhiro Matsuno
104
This library is free software; you can redistribute it and/or modify
105
it under the same terms as Perl itself.