File: Cookbook.pod

package info (click to toggle)
librouter-simple-perl 0.17-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 200 kB
  • sloc: perl: 325; makefile: 2
file content (110 lines) | stat: -rw-r--r-- 2,895 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
=head1 NAME

Router::Simple::Cookbook - The Router::Simple Cookbook

=head1 FAQ

=head2 How to create Sinatra-ish framework with Router::Simple?

Please read the following example code.

    package MySinatraish;
    use Router::Simple;
    use Plack::Request;
    
    sub import {
        my $pkg = caller(0);
        my $router = Router::Simple->new();
        my $any = sub ($$;$) {
            my ($pattern, $dest, $opt) = do {
                if (@_ == 3) {
                    my ($methods, $pattern, $code) = @_;
                    ($pattern, {code => $code}, +{method => [ map { uc $_ } @$methods ]});
                } else {
                    my ($pattern, $code) = @_;
                    ($pattern, {code => $code}, +{});
                }
            };
            $router->connect(
                $pattern,
                $dest,
                $opt,
            );
        };
        no strict 'refs';
        # any [qw/get post delete/] => '/bye' => sub { ... };
        # any '/bye' => sub { ... };
        *{"${pkg}::any"} = $any;
        *{"${pkg}::get"} = sub {
            $any->([qw/GET HEAD/], $_[0], $_[1]);
        };
        *{"${pkg}::post"} = sub {
            $any->([qw/POST/], $_[0], $_[1]);
        };
        *{"${pkg}::as_psgi_app"} = sub {
            return sub {
                if (my $p = $router->match($_[0])) {
                    [200, [], [$p->{code}->()]];
                } else {
                    [404, [], ['not found']];
                }
            }
        };
    }

    package MyApp;
    use MySinatraish;
    get '/' => sub {
        'top';
    };
    post '/new' => sub {
        'posted';
    };
    as_psgi_app;

=head2 How to switch from HTTPx::Dispatcher?

L<HTTPx::Dispatcher> is class specific declarative router.

    package MyApp::Dispatcher;
    use HTTPx::Dispatcher;
    connect '/', {controller => 'foo', action => 'bar'};
    1;

The following script is same as above.

    package MyApp::Dispatcher;
    use Router::Simple::Declare;

    my $router = router {
        connect '/', {controller => 'foo', action => 'bar'};
    };
    sub match { $router->match() }

=head2 How to use Router::Simple with non-strictly-MVC application?

    use Router::Simple::Declare;
    my $router = router {
        connect '/foo/bar/' => { 'target' => '/foobar.asp' };
        connect '/topics/:topic' => { target => '/my-topic.asp' };
        connect '/products/{Category:.*}' => { target => '/products.asp', Category => 'All' };
        connect '/zipcode/{zip:[0-9]{5,5}}' => {target => '/zipcode.asp' };
    };

You can pass the target path as destination.

=head1 AUTHOR

Tokuhiro Matsuno E<lt>tokuhirom AAJKLFJEF GMAIL COME<gt>

=head1 LICENSE

Copyright (C) Tokuhiro Matsuno

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 SEE ALSO

L<Router::Simple>