1Mojolicious::Static(3)User Contributed Perl DocumentationMojolicious::Static(3)
2
3
4
6 Mojolicious::Static - Serve static files
7
9 use Mojolicious::Static;
10
11 my $static = Mojolicious::Static->new;
12 push @{$static->classes}, 'MyApp::Controller::Foo';
13 push @{$static->paths}, '/home/sri/public';
14
16 Mojolicious::Static is a static file server with "Range",
17 "If-Modified-Since" and "If-None-Match" support, based on RFC 7232
18 <http://tools.ietf.org/html/rfc7232> and RFC 7233
19 <http://tools.ietf.org/html/rfc7233>.
20
22 Mojolicious::Static implements the following attributes.
23
24 classes
25 my $classes = $static->classes;
26 $static = $static->classes(['main']);
27
28 Classes to use for finding files in "DATA" sections with Mojo::Loader,
29 first one has the highest precedence, defaults to "main". Only files
30 with exactly one extension will be used, like "index.html". Note that
31 for files to be detected, these classes need to have already been
32 loaded and added before "warmup" is called, which usually happens
33 automatically during application startup.
34
35 # Add another class with static files in DATA section
36 push @{$static->classes}, 'Mojolicious::Plugin::Fun';
37
38 # Add another class with static files in DATA section and higher precedence
39 unshift @{$static->classes}, 'Mojolicious::Plugin::MoreFun';
40
41 extra
42 my $extra = $static->extra;
43 $static = $static->extra({'foo/bar.txt' => '/home/sri/myapp/bar.txt'});
44
45 Paths for extra files to be served from locations other than "paths",
46 such as the images used by the built-in exception and not found pages.
47 Note that extra files are only served if no better alternative could be
48 found in "paths" and "classes".
49
50 # Remove built-in favicon
51 delete $static->extra->{'favicon.ico'};
52
53 paths
54 my $paths = $static->paths;
55 $static = $static->paths(['/home/sri/public']);
56
57 Directories to serve static files from, first one has the highest
58 precedence.
59
60 # Add another "public" directory
61 push @{$static->paths}, '/home/sri/public';
62
63 # Add another "public" directory with higher precedence
64 unshift @{$static->paths}, '/home/sri/themes/blue/public';
65
67 Mojolicious::Static inherits all methods from Mojo::Base and implements
68 the following new ones.
69
70 dispatch
71 my $bool = $static->dispatch(Mojolicious::Controller->new);
72
73 Serve static file for Mojolicious::Controller object.
74
75 file
76 my $asset = $static->file('images/logo.png');
77 my $asset = $static->file('../lib/MyApp.pm');
78
79 Build Mojo::Asset::File or Mojo::Asset::Memory object for a file,
80 relative to "paths" or from "classes", or return "undef" if it doesn't
81 exist. Note that this method uses a relative path, but does not protect
82 from traversing to parent directories.
83
84 my $content = $static->file('foo/bar.html')->slurp;
85
86 is_fresh
87 my $bool = $static->is_fresh(Mojolicious::Controller->new, {etag => 'abc'});
88
89 Check freshness of request by comparing the "If-None-Match" and
90 "If-Modified-Since" request headers to the "ETag" and "Last-Modified"
91 response headers.
92
93 These options are currently available:
94
95 etag
96 etag => 'abc'
97
98 Add "ETag" header before comparing.
99
100 last_modified
101 last_modified => $epoch
102
103 Add "Last-Modified" header before comparing.
104
105 serve
106 my $bool = $static->serve(Mojolicious::Controller->new, 'images/logo.png');
107 my $bool = $static->serve(Mojolicious::Controller->new, '../lib/MyApp.pm');
108
109 Serve a specific file, relative to "paths" or from "classes". Note that
110 this method uses a relative path, but does not protect from traversing
111 to parent directories.
112
113 serve_asset
114 $static->serve_asset(Mojolicious::Controller->new, Mojo::Asset::File->new);
115
116 Serve a Mojo::Asset::File or Mojo::Asset::Memory object with "Range",
117 "If-Modified-Since" and "If-None-Match" support.
118
119 warmup
120 $static->warmup;
121
122 Prepare static files from "classes" for future use.
123
125 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
126
127
128
129perl v5.28.1 2018-11-22 Mojolicious::Static(3)