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 host names (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 Ruby's
27 Rack::URLMap.
28
30 map
31 $urlmap->map("/foo" => $app);
32 $urlmap->map("http://bar.example.com/" => $another_app);
33
34 Maps URL path or an absolute URL to a PSGI application. The match
35 order is sorted by host name length and then path length (longest
36 strings first).
37
38 URL paths need to match from the beginning and should match
39 completely until the path separator (or the end of the path). For
40 example, if you register the path "/foo", it will match with the
41 request "/foo", "/foo/" or "/foo/bar" but it won't match with
42 "/foox".
43
44 Mapping URLs with host names is also possible, and in that case the
45 URL mapping works like a virtual host.
46
47 Mappings will nest. If $app is already mapped to "/baz" it will
48 match a request for "/foo/baz" but not "/foo". See "HOW THIS WORKS"
49 for more details.
50
51 mount
52 Alias for "map".
53
54 to_app
55 my $handler = $urlmap->to_app;
56
57 Returns the PSGI application code reference. Note that the
58 Plack::App::URLMap object is callable (by overloading the code
59 dereference), so returning the object itself as a PSGI application
60 should also work.
61
63 If you "map" (or "mount" with Plack::Builder) N applications,
64 Plack::App::URLMap will need to at most iterate through N paths to
65 match incoming requests.
66
67 It is a good idea to use "map" only for a known, limited amount of
68 applications, since mounting hundreds of applications could affect
69 runtime request performance.
70
72 You can set the environment variable "PLACK_URLMAP_DEBUG" to see how
73 this application matches with the incoming request host names and
74 paths.
75
77 This application works by fixing "SCRIPT_NAME" and "PATH_INFO" before
78 dispatching the incoming request to the relocated applications.
79
80 Say you have a Wiki application that takes "/index" and "/page/*" and
81 makes a PSGI application $wiki_app out of it, using one of supported
82 web frameworks, you can put the whole application under "/wiki" by:
83
84 # MyWikiApp looks at PATH_INFO and handles /index and /page/*
85 my $wiki_app = sub { MyWikiApp->run(@_) };
86
87 use Plack::App::URLMap;
88 my $app = Plack::App::URLMap->new;
89 $app->mount("/wiki" => $wiki_app);
90
91 When a request comes in with "PATH_INFO" set to "/wiki/page/foo", the
92 URLMap application $app strips the "/wiki" part from "PATH_INFO" and
93 appends that to "SCRIPT_NAME".
94
95 That way, if the $app is mounted under the root (i.e. "SCRIPT_NAME" is
96 "") with standalone web servers like Starman, "SCRIPT_NAME" is now
97 locally set to "/wiki" and "PATH_INFO" is changed to "/page/foo" when
98 $wiki_app gets called.
99
101 Tatsuhiko Miyagawa
102
104 Plack::Builder
105
106
107
108perl v5.32.0 2020-12-02 Plack::App::URLMap(3)