1 package Catalyst::Request;
4 use base 'Class::Accessor::Fast';
6 __PACKAGE__->mk_accessors(
7 qw/action address arguments body base cookies headers hostname match
8 method parameters path snippets uploads/
13 *params = \¶meters;
15 sub content_encoding { shift->headers->content_encoding(@_) }
16 sub content_length { shift->headers->content_length(@_) }
17 sub content_type { shift->headers->content_type(@_) }
18 sub header { shift->headers->header(@_) }
19 sub referer { shift->headers->referer(@_) }
20 sub user_agent { shift->headers->user_agent(@_) }
23 my ( $self, $map, $values ) = @_;
25 while ( my ( $name, $value ) = splice( @{$values}, 0, 2 ) ) {
27 if ( exists $map->{$name} ) {
28 for ( $map->{$name} ) {
29 $_ = [$_] unless ref($_) eq "ARRAY";
34 $map->{$name} = $value;
41 Catalyst::Request - Catalyst Request Class
53 $req->content_encoding;
77 This is the Catalyst Request class, which provides a set of accessors to the
78 request data. The request object is prepared by the specialized Catalyst
79 Engine module thus hiding the details of the particular engine implementation.
88 Contains the requested action.
90 print $c->request->action;
94 Contains the remote address.
96 print $c->request->address
100 Shortcut for arguments
102 =item $req->arguments
104 Returns a reference to an array containing the arguments.
106 print $c->request->arguments->[0];
110 Contains the url base. This will always have a trailing slash.
114 Contains the message body of the request unless Content-Type is
115 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
117 print $c->request->body
119 =item $req->content_encoding
121 Shortcut to $req->headers->content_encoding
123 =item $req->content_length
125 Shortcut to $req->headers->content_length
127 =item $req->content_type
129 Shortcut to $req->headers->content_type
133 Returns a reference to a hash containing the cookies.
135 print $c->request->cookies->{mycookie}->value;
139 Shortcut to $req->headers->header
143 Returns an L<HTTP::Headers> object containing the headers.
145 print $c->request->headers->header('X-Catalyst');
149 Contains the hostname of the remote user.
151 print $c->request->hostname
155 Shortcut for $req->body.
159 This contains be the matching part of a regexp action. otherwise it
160 returns the same as 'action'.
162 print $c->request->match;
166 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
168 print $c->request->method;
172 Get request parameters with a CGI.pm like param method.
174 $value = $c->request->param('foo');
175 @values = $c->request->param('foo');
176 @params = $c->request->param;
184 return keys %{ $self->parameters };
189 unless ( exists $self->parameters->{$param} ) {
190 return wantarray ? () : undef;
193 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
195 ? @{ $self->parameters->{$param} }
196 : $self->parameters->{$param}->[0];
200 ? ( $self->parameters->{$param} )
201 : $self->parameters->{$param};
207 Shortcut for $req->parameters.
209 =item $req->parameters
211 Returns a reference to a hash containing parameters. Values can
212 be either a scalar or a arrayref containing scalars.
214 print $c->request->parameters->{field};
215 print $c->request->parameters->{field}->[0];
221 print $c->request->path;
225 Shortcut to $req->headers->referer. Referring page.
229 Returns a reference to an array containing regex snippets.
231 my @snippets = @{ $c->request->snippets };
235 A convenient method to $req->uploads.
237 $upload = $c->request->upload('field');
238 @uploads = $c->request->upload('field');
239 @fields = $c->request->upload;
241 for my $upload ( $c->request->upload('field') ) {
242 print $upload->filename;
251 return keys %{ $self->uploads };
256 unless ( exists $self->uploads->{$upload} ) {
257 return wantarray ? () : undef;
260 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
262 ? @{ $self->uploads->{$upload} }
263 : $self->uploads->{$upload}->[0];
267 ? ( $self->uploads->{$upload} )
268 : $self->uploads->{$upload};
274 Returns a reference to a hash containing uploads. Values can be either a
275 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
277 my $upload = $c->request->uploads->{field};
278 my $upload = $c->request->uploads->{field}->[0];
280 =item $req->user_agent
282 Shortcut to $req->headers->user_agent. User Agent version string.
288 Sebastian Riedel, C<sri@cpan.org>
289 Marcus Ramberg, C<mramberg@cpan.org>
293 This program is free software, you can redistribute it and/or modify
294 it under the same terms as Perl itself.