new document reviewing catalyst UTF8 changes
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ActionRole / Scheme.pm
CommitLineData
342d2169 1package Catalyst::ActionRole::Scheme;
2
3use Moose::Role;
4
5requires 'match', 'match_captures', 'list_extra_info';
6
7around ['match','match_captures'] => sub {
8 my ($orig, $self, $ctx, @args) = @_;
9 my $request_scheme = lc($ctx->req->env->{'psgi.url_scheme'});
10 my $match_scheme = lc($self->scheme||'');
11
12 return $request_scheme eq $match_scheme ? $self->$orig($ctx, @args) : 0;
13};
14
15around 'list_extra_info' => sub {
16 my ($orig, $self, @args) = @_;
17 return {
18 %{ $self->$orig(@args) },
19 Scheme => $self->attributes->{Scheme}[0]||'',
20 };
21};
22
231;
24
25=head1 NAME
26
27Catalyst::ActionRole::ConsumesContent - Match on HTTP Request Content-Type
28
29=head1 SYNOPSIS
30
31 package MyApp::Web::Controller::MyController;
32
33 use base 'Catalyst::Controller';
34
35 sub start : POST Chained('/') CaptureArg(0) { ... }
36
37 sub is_json : Chained('start') Consumes('application/json') { ... }
38 sub is_urlencoded : Chained('start') Consumes('application/x-www-form-urlencoded') { ... }
39 sub is_multipart : Chained('start') Consumes('multipart/form-data') { ... }
40
41 ## Alternatively, for common types...
42
43 sub is_json : Chained('start') Consume(JSON) { ... }
44 sub is_urlencoded : Chained('start') Consumes(UrlEncoded) { ... }
45 sub is_multipart : Chained('start') Consumes(Multipart) { ... }
46
47 ## Or allow more than one type
48
49 sub is_more_than_one
50 : Chained('start')
51 : Consumes('application/x-www-form-urlencoded')
52 : Consumes('multipart/form-data')
53 {
54 ## ...
55 }
56
57 1;
58
59=head1 DESCRIPTION
60
61This is an action role that lets your L<Catalyst::Action> match on the content
62type of the incoming request.
63
64Generally when there's a PUT or POST request, there's a request content body
65with a matching MIME content type. Commonly this will be one of the types
66used with classic HTML forms ('application/x-www-form-urlencoded' for example)
67but there's nothing stopping you specifying any valid content type.
68
69For matching purposes, we match strings but the casing is insensitive.
70
71=head1 REQUIRES
72
73This role requires the following methods in the consuming class.
74
75=head2 match
76
77=head2 match_captures
78
79Returns 1 if the action matches the existing request and zero if not.
80
81=head1 METHODS
82
83This role defines the following methods
84
85=head2 match
86
87=head2 match_captures
88
89Around method modifier that return 1 if the request content type matches one of the
90allowed content types (see L</http_methods>) and zero otherwise.
91
92=head2 allowed_content_types
93
94An array of strings that are the allowed content types for matching this action.
95
96=head2 can_consume
97
98Boolean. Does the current request match content type with what this actionrole
99can consume?
100
101=head2 list_extra_info
102
103Add the accepted content type to the debug screen.
104
105=head1 AUTHORS
106
107Catalyst Contributors, see Catalyst.pm
108
109=head1 COPYRIGHT
110
111This library is free software. You can redistribute it and/or modify it under
112the same terms as Perl itself.
113
114=cut