File Coverage

blib/lib/Catalyst/Controller/Public.pm
Criterion Covered Total %
statement 5 5 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod 1 1 100.0
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Catalyst::Controller::Public;
2              
3 1     1   1412884 use Moose;
  1         2  
  1         6  
4             extends 'Catalyst::Controller';
5              
6             our $VERSION = '0.003';
7              
8             has show_debugging => (is=>'ro', required=>1, default=>sub {0});
9             has cache_control => (is=>'ro', isa=>'Str', predicate=>'has_cache_control');
10             has content_types => (is=>'ro', isa=>'ArrayRef[Str]', predicate=>'has_content_types');
11              
12             has at => (
13             is=>'ro',
14             isa=>'Str',
15             required=>1,
16             default=>'/:namespace/:args');
17              
18             after 'register_actions' => sub {
19             my ($self, $app) = @_;
20             my $action = $self->create_action(
21             name => 'serve_file',
22             code => sub { },
23             reverse => $self->action_namespace . '/' .'serve_file',
24             namespace => $self->action_namespace,
25             class => ref($self),
26             attributes => {
27             Path => [ $self->action_namespace ],
28             Does => ['Catalyst::ActionRole::Public'],
29             At => [$self->at],
30             ShowDebugging => [$self->show_debugging],
31             ( $self->has_cache_control ? (CacheControl => [$self->cache_control]) : ()),
32             ( $self->has_content_types ? (ContentTypes => $self->content_types)
33             : ()),
34             });
35              
36             $app->dispatcher->register( $app, $action );
37             };
38              
39             sub uri_args {
40 1     1 1 5078 my $self = shift;
41 1         10 return $self->action_for('serve_file'), @_;
42             }
43              
44             __PACKAGE__->meta->make_immutable;
45              
46             1;
47              
48             =head1 NAME
49              
50             Catalyst::Controller::Public - mount a public url to files in your Catalyst project
51              
52             =head1 SYNOPSIS
53              
54             package MyApp::Controller::Public;
55              
56             use Moose;
57             extends 'Catalyst::Controller::Public';
58              
59             __PACKAGE__->meta->make_immutable;
60              
61             Will create an action that from URL 'localhost/public/a/b/c/d.js' will serve
62             file $c->{root} . '/public' . '/a/b/c/d.js'. Will also set content type, length
63             and Last-Modified HTTP headers as needed. If the file does not exist, will not
64             match (allowing possibly other actions to match).
65              
66             You can create a URL for a static file programmtically via the following:
67              
68             sub myaction :Local {
69             my ($self, $c) = @_;
70             my $static_url = $c->uri_for(controller('Public')->uri_args('example.txt'));
71             }
72              
73             =head1 DESCRIPTION
74              
75             This is a simple controller that uses L<Catalyst::ActionRole::Public>
76             to create a single public folder for you webapplication, doing as much
77             of the right thing as possible. Useful for starting a new project, although
78             I imagine as time goes on you'll probably want something stronger.
79              
80             This controller doesn't do anything like compile LESS to CSS, etc. If you
81             are looking for that you might find L<Catalyst::Controller::SimpleCAS> has
82             more power for what you wish. This is really aimed at helping people move
83             away from L<Catalyst::Plugin::Static::Simple> which I really don't want
84             to support anymore :)
85              
86             =head1 METHODS
87              
88             This controller defines the following methods
89              
90             =head2 uri_args
91              
92             Used as a helper to correctly generate a URI. For example:
93              
94             sub myaction :Local {
95             my ($self, $c) = @_;
96             my $static_url = $c->uri_for(controller('Public')
97             ->uri_args('example.txt'));
98             }
99              
100             =head1 ATTRIBUTES
101              
102             This controller defines the following configuration attributes. They
103             are pretty much all just wrappers for the same configuration options for
104             the L<Catalyst::ActionRole::Public>
105              
106             =head2 at
107              
108             Template used to control how we build the path to find your public file.
109             You probably want to leave this alone if you are seeking the most simple
110             thing (which this controller is aimed at). See the documentation for 'At'
111             over in L<Catalyst::ActionRole::Public> if you really need to mess with this
112             (and you might want the increased control that action role gives you anyway.
113              
114             =head2 content_types
115              
116             Content types that we allow to be served. By default we allow all standard
117             types (might be more than you want, if your public directory contains things
118             you don't want the public to see.
119              
120             =head2 show_debugging
121              
122             Enabled developer debugging output. Default to 0 (false, no debugging). Change
123             to 1 if you want extended debugging info.
124              
125             =head2 cache_control
126              
127             Used to set the Cache-Control HTTP header (useful for caching your static assets).
128              
129             Example values "public, max-age=600"
130              
131             =head1 AUTHOR
132            
133             John Napiorkowski L<email:jjnapiork@cpan.org>
134            
135             =head1 SEE ALSO
136            
137             L<Catalyst>, L<Catalyst::Controller>, L<Plack::App::Directory>,
138             L<Catalyst::Controller::Assets>. L<Catalyst::Controller::SimpleCAS>
139            
140             =head1 COPYRIGHT & LICENSE
141            
142             Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org>
143            
144             This library is free software; you can redistribute it and/or modify it under
145             the same terms as Perl itself.
146              
147             =cut