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(@_) }
24 Catalyst::Request - Catalyst Request Class
38 $req->content_encoding;
62 This is the Catalyst Request class, which provides a set of accessors to the
63 request data. The request object is prepared by the specialized Catalyst
64 Engine module thus hiding the details of the particular engine implementation.
73 Contains the requested action.
75 print $c->request->action;
79 Contains the remote address.
81 print $c->request->address
85 Shortcut for arguments
89 Returns a reference to an array containing the arguments.
91 print $c->request->arguments->[0];
95 Contains the url base. This will always have a trailing slash.
99 Contains the message body of the request unless Content-Type is
100 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
102 print $c->request->body
104 =item $req->body_length
106 Returns the length of body in bytes.
108 print $c->request->body_length
117 return 0 unless $self->body;
118 return length($self->body);
123 Returns a reference to body.
129 return \$self->{body};
132 =item $req->content_encoding
134 Shortcut to $req->headers->content_encoding
136 =item $req->content_length
138 Shortcut to $req->headers->content_length
140 =item $req->content_type
142 Shortcut to $req->headers->content_type
146 Returns a reference to a hash containing the cookies.
148 print $c->request->cookies->{mycookie}->value;
152 Shortcut to $req->headers->header
156 Returns an L<HTTP::Headers> object containing the headers.
158 print $c->request->headers->header('X-Catalyst');
162 Contains the hostname of the remote user.
164 print $c->request->hostname
168 Shortcut for $req->body.
172 This contains be the matching part of a regexp action. otherwise it
173 returns the same as 'action'.
175 print $c->request->match;
179 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
181 print $c->request->method;
185 Get request parameters with a CGI.pm like param method.
187 $value = $c->request->param('foo');
188 @values = $c->request->param('foo');
189 @params = $c->request->param;
197 return keys %{ $self->parameters };
200 if ( @_ == 1 and ref( $_[0] ) eq 'ARRAY' ) {
202 while ( my ( $field, $value ) = splice( @{ $_[0] }, 0, 2 ) ) {
204 if ( exists $self->parameters->{$field} ) {
205 for ( $self->parameters->{$field} ) {
206 $_ = [$_] unless ref($_) eq "ARRAY";
211 $self->parameters->{$field} = $value;
220 unless ( exists $self->parameters->{$param} ) {
221 return wantarray ? () : undef;
224 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
226 ? @{ $self->parameters->{$param} }
227 : $self->parameters->{$param}->[0];
231 ? ( $self->parameters->{$param} )
232 : $self->parameters->{$param};
239 Shortcut for $req->parameters.
241 =item $req->parameters
243 Returns a reference to a hash containing parameters. Values can
244 be either a scalar or a arrayref containing scalars.
246 print $c->request->parameters->{field};
247 print $c->request->parameters->{field}->[0];
253 print $c->request->path;
257 Shortcut to $req->headers->referer. Referring page.
261 Returns a reference to an array containing regex snippets.
263 my @snippets = @{ $c->request->snippets };
267 A convenient method to $req->uploads.
269 $upload = $c->request->upload('field');
270 @uploads = $c->request->upload('field');
271 @fields = $c->request->upload;
273 for my $upload ( $c->request->upload('field') ) {
274 print $upload->filename;
283 return keys %{ $self->uploads };
286 if ( @_ == 1 and ref( $_[0] ) eq 'ARRAY' ) {
288 while ( my ( $field, $upload ) = splice( @{ $_[0] }, 0, 2 ) ) {
290 if ( exists $self->uploads->{$field} ) {
291 for ( $self->uploads->{$field} ) {
292 $_ = [$_] unless ref($_) eq "ARRAY";
293 push( @$_, $upload );
297 $self->uploads->{$field} = $upload;
306 unless ( exists $self->uploads->{$upload} ) {
307 return wantarray ? () : undef;
310 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
312 ? @{ $self->uploads->{$upload} }
313 : $self->uploads->{$upload}->[0];
317 ? ( $self->uploads->{$upload} )
318 : $self->uploads->{$upload};
325 Returns a reference to a hash containing uploads. Values can be either a
326 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
328 my $upload = $c->request->uploads->{field};
329 my $upload = $c->request->uploads->{field}->[0];
331 =item $req->user_agent
333 Shortcut to $req->headers->user_agent. User Agent version string.
339 Sebastian Riedel, C<sri@cpan.org>
340 Marcus Ramberg, C<mramberg@cpan.org>
344 This program is free software, you can redistribute it and/or modify
345 it under the same terms as Perl itself.