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
36 $req->content_encoding;
60 This is the Catalyst Request class, which provides a set of accessors to the
61 request data. The request object is prepared by the specialized Catalyst
62 Engine module thus hiding the details of the particular engine implementation.
71 Contains the requested action.
73 print $c->request->action;
77 Contains the remote address.
79 print $c->request->address
83 Shortcut for arguments
87 Returns a reference to an array containing the arguments.
89 print $c->request->arguments->[0];
93 Contains the url base. This will always have a trailing slash.
97 Contains the message body of the request unless Content-Type is
98 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
100 print $c->request->body
102 =item $req->content_encoding
104 Shortcut to $req->headers->content_encoding
106 =item $req->content_length
108 Shortcut to $req->headers->content_length
110 =item $req->content_type
112 Shortcut to $req->headers->content_type
116 Returns a reference to a hash containing the cookies.
118 print $c->request->cookies->{mycookie}->value;
122 Shortcut to $req->headers->header
126 Returns an L<HTTP::Headers> object containing the headers.
128 print $c->request->headers->header('X-Catalyst');
132 Contains the hostname of the remote user.
134 print $c->request->hostname
138 Shortcut for $req->body.
142 This contains be the matching part of a regexp action. otherwise it
143 returns the same as 'action'.
145 print $c->request->match;
149 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
151 print $c->request->method;
155 Get request parameters with a CGI.pm like param method.
157 $value = $c->request->param('foo');
158 @values = $c->request->param('foo');
159 @params = $c->request->param;
167 return keys %{ $self->parameters };
170 if ( @_ == 1 and ref( $_[0] ) eq 'ARRAY' ) {
172 while ( my ( $field, $value ) = splice( @{ $_[0] }, 0, 2 ) ) {
174 if ( exists $self->parameters->{$field} ) {
175 for ( $self->parameters->{$field} ) {
176 $_ = [$_] unless ref($_) eq "ARRAY";
181 $self->parameters->{$field} = $value;
190 unless ( exists $self->parameters->{$param} ) {
191 return wantarray ? () : undef;
194 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
196 ? @{ $self->parameters->{$param} }
197 : $self->parameters->{$param}->[0];
201 ? ( $self->parameters->{$param} )
202 : $self->parameters->{$param};
209 Shortcut for $req->parameters.
211 =item $req->parameters
213 Returns a reference to a hash containing parameters. Values can
214 be either a scalar or a arrayref containing scalars.
216 print $c->request->parameters->{field};
217 print $c->request->parameters->{field}->[0];
223 print $c->request->path;
227 Shortcut to $req->headers->referer. Referring page.
231 Returns a reference to an array containing regex snippets.
233 my @snippets = @{ $c->request->snippets };
237 A convenient method to $req->uploads.
239 $upload = $c->request->upload('field');
240 @uploads = $c->request->upload('field');
241 @fields = $c->request->upload;
243 for my $upload ( $c->request->upload('field') ) {
244 print $upload->filename;
253 return keys %{ $self->uploads };
256 if ( @_ == 1 and ref( $_[0] ) eq 'ARRAY' ) {
258 while ( my ( $field, $upload ) = splice( @{ $_[0] }, 0, 2 ) ) {
260 if ( exists $self->uploads->{$field} ) {
261 for ( $self->uploads->{$field} ) {
262 $_ = [$_] unless ref($_) eq "ARRAY";
263 push( @$_, $upload );
267 $self->uploads->{$field} = $upload;
276 unless ( exists $self->uploads->{$upload} ) {
277 return wantarray ? () : undef;
280 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
282 ? @{ $self->uploads->{$upload} }
283 : $self->uploads->{$upload}->[0];
287 ? ( $self->uploads->{$upload} )
288 : $self->uploads->{$upload};
295 Returns a reference to a hash containing uploads. Values can be either a
296 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
298 my $upload = $c->request->uploads->{field};
299 my $upload = $c->request->uploads->{field}->[0];
301 =item $req->user_agent
303 Shortcut to $req->headers->user_agent. User Agent version string.
309 Sebastian Riedel, C<sri@cpan.org>
310 Marcus Ramberg, C<mramberg@cpan.org>
314 This program is free software, you can redistribute it and/or modify
315 it under the same terms as Perl itself.