1Router::Simple::CookbooUks(e3r)Contributed Perl DocumentRaotuitoenr::Simple::Cookbook(3)
2
3
4

NAME

6       Router::Simple::Cookbook - The Router::Simple Cookbook
7

FAQ

9   How to create Sinatra-ish framework with Router::Simple?
10       Please read the following example code.
11
12           package MySinatraish;
13           use Router::Simple;
14           use Plack::Request;
15
16           sub import {
17               my $pkg = caller(0);
18               my $router = Router::Simple->new();
19               my $any = sub ($$;$) {
20                   my ($pattern, $dest, $opt) = do {
21                       if (@_ == 3) {
22                           my ($methods, $pattern, $code) = @_;
23                           ($pattern, {code => $code}, +{method => [ map { uc $_ } @$methods ]});
24                       } else {
25                           my ($pattern, $code) = @_;
26                           ($pattern, {code => $code}, +{});
27                       }
28                   };
29                   $router->connect(
30                       $pattern,
31                       $dest,
32                       $opt,
33                   );
34               };
35               no strict 'refs';
36               # any [qw/get post delete/] => '/bye' => sub { ... };
37               # any '/bye' => sub { ... };
38               *{"${pkg}::any"} = $any;
39               *{"${pkg}::get"} = sub {
40                   $any->([qw/GET HEAD/], $_[0], $_[1]);
41               };
42               *{"${pkg}::post"} = sub {
43                   $any->([qw/POST/], $_[0], $_[1]);
44               };
45               *{"${pkg}::as_psgi_app"} = sub {
46                   return sub {
47                       if (my $p = $router->match($_[0])) {
48                           [200, [], [$p->{code}->()]];
49                       } else {
50                           [404, [], ['not found']];
51                       }
52                   }
53               };
54           }
55
56           package MyApp;
57           use MySinatraish;
58           get '/' => sub {
59               'top';
60           };
61           post '/new' => sub {
62               'posted';
63           };
64           as_psgi_app;
65
66   How to switch from HTTPx::Dispatcher?
67       HTTPx::Dispatcher is class specific declarative router.
68
69           package MyApp::Dispatcher;
70           use HTTPx::Dispatcher;
71           connect '/', {controller => 'foo', action => 'bar'};
72           1;
73
74       The following script is same as above.
75
76           package MyApp::Dispatcher;
77           use Router::Simple::Declare;
78
79           my $router = router {
80               connect '/', {controller => 'foo', action => 'bar'};
81           };
82           sub match { $router->match() }
83
84   How to use Router::Simple with non-strictly-MVC application?
85           use Router::Simple::Declare;
86           my $router = router {
87               connect '/foo/bar/' => { 'target' => '/foobar.asp' };
88               connect '/topics/:topic' => { target => '/my-topic.asp' };
89               connect '/products/{Category:.*}' => { target => '/products.asp', Category => 'All' };
90               connect '/zipcode/{zip:[0-9]{5,5}}' => {target => '/zipcode.asp' };
91           };
92
93       You can pass the target path as destination.
94

AUTHOR

96       Tokuhiro Matsuno <tokuhirom AAJKLFJEF GMAIL COM>
97

LICENSE

99       Copyright (C) Tokuhiro Matsuno
100
101       This library is free software; you can redistribute it and/or modify it
102       under the same terms as Perl itself.
103

SEE ALSO

105       Router::Simple
106
107
108
109perl v5.34.0                      2022-01-21       Router::Simple::Cookbook(3)
Impressum