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
51 $req->content_encoding;
75 This is the Catalyst Request class, which provides a set of accessors to the
76 request data. The request object is prepared by the specialized Catalyst
77 Engine module thus hiding the details of the particular engine implementation.
86 Contains the requested action.
88 print $c->request->action;
92 Contains the remote address.
94 print $c->request->address
98 Shortcut for arguments
100 =item $req->arguments
102 Returns a reference to an array containing the arguments.
104 print $c->request->arguments->[0];
108 Contains the url base. This will always have a trailing slash.
110 =item $req->content_encoding
112 Shortcut to $req->headers->content_encoding
114 =item $req->content_length
116 Shortcut to $req->headers->content_length
118 =item $req->content_type
120 Shortcut to $req->headers->content_type
124 Returns a reference to a hash containing the cookies.
126 print $c->request->cookies->{mycookie}->value;
130 Shortcut to $req->headers->header
134 Returns an L<HTTP::Headers> object containing the headers.
136 print $c->request->headers->header('X-Catalyst');
140 Contains the hostname of the remote user.
142 print $c->request->hostname
146 Contains the message body of the request unless Content-Type is
147 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
149 print $c->request->input
153 This contains be the matching part of a regexp action. otherwise it
154 returns the same as 'action'.
156 print $c->request->match;
160 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
162 print $c->request->method;
166 Get request parameters with a CGI.pm like param method.
168 $value = $c->request->param('foo');
169 @values = $c->request->param('foo');
170 @params = $c->request->param;
178 return keys %{ $self->parameters };
183 unless ( exists $self->parameters->{$param} ) {
184 return wantarray ? () : undef;
187 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
189 ? @{ $self->parameters->{$param} }
190 : $self->parameters->{$param}->[0];
194 ? ( $self->parameters->{$param} )
195 : $self->parameters->{$param};
201 Shortcut for $req->parameters.
203 =item $req->parameters
205 Returns a reference to a hash containing parameters. Values can
206 be either a scalar or a arrayref containing scalars.
208 print $c->request->parameters->{field};
209 print $c->request->parameters->{field}->[0];
215 print $c->request->path;
219 Shortcut to $req->headers->referer. Referring page.
223 Returns a reference to an array containing regex snippets.
225 my @snippets = @{ $c->request->snippets };
229 A convenient method to $req->uploads.
231 $upload = $c->request->upload('field');
232 @uploads = $c->request->upload('field');
233 @fields = $c->request->upload;
235 for my $upload ( $c->request->upload('field') ) {
236 print $upload->filename;
245 return keys %{ $self->uploads };
250 unless ( exists $self->uploads->{$upload} ) {
251 return wantarray ? () : undef;
254 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
256 ? @{ $self->uploads->{$upload} }
257 : $self->uploads->{$upload}->[0];
261 ? ( $self->uploads->{$upload} )
262 : $self->uploads->{$upload};
268 Returns a reference to a hash containing uploads. Values can be either a
269 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
271 my $upload = $c->request->uploads->{field};
272 my $upload = $c->request->uploads->{field}->[0];
274 =item $req->user_agent
276 Shortcut to $req->headers->user_agent. User Agent version string.
282 Sebastian Riedel, C<sri@cpan.org>
283 Marcus Ramberg, C<mramberg@cpan.org>
287 This program is free software, you can redistribute it and/or modify
288 it under the same terms as Perl itself.