1 package Catalyst::Request;
4 use base 'Class::Accessor::Fast';
6 __PACKAGE__->mk_accessors(
7 qw/action address arguments base cookies headers input hostname match
8 method parameters path snippets uploads/
12 *params = \¶meters;
14 sub content_encoding { shift->headers->content_encoding(@_) }
15 sub content_length { shift->headers->content_length(@_) }
16 sub content_type { shift->headers->content_type(@_) }
17 sub header { shift->headers->header(@_) }
18 sub referer { shift->headers->referer(@_) }
19 sub user_agent { shift->headers->user_agent(@_) }
22 my ( $self, $map, $values ) = @_;
24 while ( my ( $name, $value ) = splice( @{$values}, 0, 2 ) ) {
26 if ( exists $map->{$name} ) {
27 for ( $map->{$name} ) {
28 $_ = [$_] unless ref($_) eq "ARRAY";
33 $map->{$name} = $value;
40 Catalyst::Request - Catalyst Request Class
52 $req->content_encoding;
76 This is the Catalyst Request class, which provides a set of accessors to the
77 request data. The request object is prepared by the specialized Catalyst
78 Engine module thus hiding the details of the particular engine implementation.
87 Contains the requested action.
89 print $c->request->action;
93 Contains the remote address.
95 print $c->request->address
99 Shortcut for arguments
101 =item $req->arguments
103 Returns a reference to an array containing the arguments.
105 print $c->request->arguments->[0];
109 Contains the url base. This will always have a trailing slash.
113 Shortcut for $req->input.
115 =item $req->content_encoding
117 Shortcut to $req->headers->content_encoding
119 =item $req->content_length
121 Shortcut to $req->headers->content_length
123 =item $req->content_type
125 Shortcut to $req->headers->content_type
129 Returns a reference to a hash containing the cookies.
131 print $c->request->cookies->{mycookie}->value;
135 Shortcut to $req->headers->header
139 Returns an L<HTTP::Headers> object containing the headers.
141 print $c->request->headers->header('X-Catalyst');
145 Contains the hostname of the remote user.
147 print $c->request->hostname
151 Contains the message body of the request unless Content-Type is
152 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
154 print $c->request->input
158 This contains be the matching part of a regexp action. otherwise it
159 returns the same as 'action'.
161 print $c->request->match;
165 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
167 print $c->request->method;
171 Get request parameters with a CGI.pm like param method.
173 $value = $c->request->param('foo');
174 @values = $c->request->param('foo');
175 @params = $c->request->param;
183 return keys %{ $self->parameters };
188 unless ( exists $self->parameters->{$param} ) {
189 return wantarray ? () : undef;
192 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
194 ? @{ $self->parameters->{$param} }
195 : $self->parameters->{$param}->[0];
199 ? ( $self->parameters->{$param} )
200 : $self->parameters->{$param};
206 Shortcut for $req->parameters.
208 =item $req->parameters
210 Returns a reference to a hash containing parameters. Values can
211 be either a scalar or a arrayref containing scalars.
213 print $c->request->parameters->{field};
214 print $c->request->parameters->{field}->[0];
220 print $c->request->path;
224 Shortcut to $req->headers->referer. Referring page.
228 Returns a reference to an array containing regex snippets.
230 my @snippets = @{ $c->request->snippets };
234 A convenient method to $req->uploads.
236 $upload = $c->request->upload('field');
237 @uploads = $c->request->upload('field');
238 @fields = $c->request->upload;
240 for my $upload ( $c->request->upload('field') ) {
241 print $upload->filename;
250 return keys %{ $self->uploads };
255 unless ( exists $self->uploads->{$upload} ) {
256 return wantarray ? () : undef;
259 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
261 ? @{ $self->uploads->{$upload} }
262 : $self->uploads->{$upload}->[0];
266 ? ( $self->uploads->{$upload} )
267 : $self->uploads->{$upload};
273 Returns a reference to a hash containing uploads. Values can be either a
274 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
276 my $upload = $c->request->uploads->{field};
277 my $upload = $c->request->uploads->{field}->[0];
279 =item $req->user_agent
281 Shortcut to $req->headers->user_agent. User Agent version string.
287 Sebastian Riedel, C<sri@cpan.org>
288 Marcus Ramberg, C<mramberg@cpan.org>
292 This program is free software, you can redistribute it and/or modify
293 it under the same terms as Perl itself.