~ubuntu-branches/ubuntu/trusty/librouter-simple-perl/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Router/Simple/Cookbook.pod

  • Committer: Package Import Robot
  • Author(s): Florian Schlichting
  • Date: 2012-06-04 21:31:28 UTC
  • Revision ID: package-import@ubuntu.com-20120604213128-tmi08jxwpq0mnuh5
Tags: upstream-0.09
ImportĀ upstreamĀ versionĀ 0.09

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=head1 NAME
 
2
 
 
3
Router::Simple::Cookbook - The Router::Simple Cookbook
 
4
 
 
5
=head1 FAQ
 
6
 
 
7
=head2 How to create Sinatra-ish framework with Router::Simple?
 
8
 
 
9
Please read the following example code.
 
10
 
 
11
    package MySinatraish;
 
12
    use Router::Simple;
 
13
    use Plack::Request;
 
14
    
 
15
    sub import {
 
16
        my $pkg = caller(0);
 
17
        my $router = Router::Simple->new();
 
18
        my $any = sub ($$;$) {
 
19
            my ($pattern, $dest, $opt) = do {
 
20
                if (@_ == 3) {
 
21
                    my ($methods, $pattern, $code) = @_;
 
22
                    ($pattern, {code => $code}, +{method => [ map { uc $_ } @$methods ]});
 
23
                } else {
 
24
                    my ($pattern, $code) = @_;
 
25
                    ($pattern, {code => $code}, +{});
 
26
                }
 
27
            };
 
28
            $router->connect(
 
29
                $pattern,
 
30
                $dest,
 
31
                $opt,
 
32
            );
 
33
        };
 
34
        no strict 'refs';
 
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]);
 
40
        };
 
41
        *{"${pkg}::post"} = sub {
 
42
            $any->([qw/POST/], $_[0], $_[1]);
 
43
        };
 
44
        *{"${pkg}::as_psgi_app"} = sub {
 
45
            return sub {
 
46
                if (my $p = $router->match($_[0])) {
 
47
                    [200, [], [$p->{code}->()]];
 
48
                } else {
 
49
                    [404, [], ['not found']];
 
50
                }
 
51
            }
 
52
        };
 
53
    }
 
54
 
 
55
    package MyApp;
 
56
    use MySinatraish;
 
57
    get '/' => sub {
 
58
        'top';
 
59
    };
 
60
    post '/new' => sub {
 
61
        'posted';
 
62
    };
 
63
    as_psgi_app;
 
64
 
 
65
=head2 How to switch from HTTPx::Dispatcher?
 
66
 
 
67
L<HTTPx::Dispatcher> is class specific declararative router.
 
68
 
 
69
    package MyApp::Dispatcher;
 
70
    use HTTPx::Dspatcher;
 
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
=head2 How to use Router::Simple with non-strictly-MVC application?
 
85
 
 
86
    use Router::Simple::Declare;
 
87
    my $router = router {
 
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' };
 
92
    };
 
93
 
 
94
You can pass the target path as destination.
 
95
 
 
96
=head1 AUTHOR
 
97
 
 
98
Tokuhiro Matsuno E<lt>tokuhirom AAJKLFJEF GMAIL COME<gt>
 
99
 
 
100
=head1 LICENSE
 
101
 
 
102
Copyright (C) Tokuhiro Matsuno
 
103
 
 
104
This library is free software; you can redistribute it and/or modify
 
105
it under the same terms as Perl itself.
 
106
 
 
107
=head1 SEE ALSO
 
108
 
 
109
L<Router::Simple>
 
110