1Plack::App::URLMap(3) User Contributed Perl DocumentationPlack::App::URLMap(3)
2
3
4
6 Plack::App::URLMap - Map multiple apps in different paths
7
9 use Plack::App::URLMap;
10
11 my $app1 = sub { ... };
12 my $app2 = sub { ... };
13 my $app3 = sub { ... };
14
15 my $urlmap = Plack::App::URLMap->new;
16 $urlmap->map("/" => $app1);
17 $urlmap->map("/foo" => $app2);
18 $urlmap->map("http://bar.example.com/" => $app3);
19
20 my $app = $urlmap->to_app;
21
23 Plack::App::URLMap is a PSGI application that can dispatch multiple
24 applications based on URL path and hostnames (a.k.a "virtual hosting")
25 and takes care of rewriting "SCRIPT_NAME" and "PATH_INFO" (See "HOW
26 THIS WORKS" for details). This module is inspired by Rack::URLMap.
27
29 map
30 $urlmap->map("/foo" => $app);
31 $urlmap->map("http://bar.example.com/" => $another_app);
32
33 Maps URL path or an absolute URL to a PSGI application. The match
34 order is sorted by host name length and then path length.
35
36 URL paths need to match from the beginning and should match
37 completely till the path separator (or the end of the path). For
38 example, if you register the path "/foo", it will match with the
39 request "/foo", "/foo/" or "/foo/bar" but it won't match with
40 "/foox".
41
42 Mapping URL with host names is also possible, and in that case the
43 URL mapping works like a virtual host.
44
45 Mappings will nest. If $app is already mapped to "/baz" it will
46 match a request for "/foo/baz" but not "/foo". See "HOW THIS WORKS"
47 for more details.
48
49 mount
50 Alias for "map".
51
52 to_app
53 my $handler = $urlmap->to_app;
54
55 Returns the PSGI application code reference. Note that the
56 Plack::App::URLMap object is callable (by overloading the code
57 dereference), so returning the object itself as a PSGI application
58 should also work.
59
61 You can set the environment variable "PLACK_URLMAP_DEBUG" to see how
62 this application matches with the incoming request host names and
63 paths.
64
66 This application works by fixing "SCRIPT_NAME" and "PATH_INFO" before
67 dispatching the incoming request to the relocated applications.
68
69 Say you have a Wiki application that takes "/index" and "/page/*" and
70 makes a PSGI application $wiki_app out of it, using one of supported
71 web frameworks, you can put the whole application under "/wiki" by:
72
73 # MyWikiApp looks at PATH_INFO and handles /index and /page/*
74 my $wiki_app = sub { MyWikiApp->run(@_) };
75
76 use Plack::App::URLMap;
77 my $app = Plack::App::URLMap->new;
78 $app->mount("/wiki" => $wiki_app);
79
80 When a request comes in with "PATH_INFO" set to "/wiki/page/foo", the
81 URLMap application $app strips the "/wiki" part from "PATH_INFO" and
82 appends that to "SCRIPT_NAME".
83
84 That way, if the $app is mounted under the root (i.e. "SCRIPT_NAME" is
85 "") with standalone web servers like Starman, "SCRIPT_NAME" is now
86 locally set to "/wiki" and "PATH_INFO" is changed to "/page/foo" when
87 $wiki_app gets called.
88
90 Tatsuhiko Miyagawa
91
93 Plack::Builder
94
95
96
97perl v5.12.3 2011-06-22 Plack::App::URLMap(3)