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 protocol secure snippets uploads user/
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;
64 This is the Catalyst Request class, which provides a set of accessors to the
65 request data. The request object is prepared by the specialized Catalyst
66 Engine module thus hiding the details of the particular engine implementation.
75 Contains the requested action.
77 print $c->request->action;
81 Contains the remote address.
83 print $c->request->address
87 Shortcut for arguments
91 Returns a reference to an array containing the arguments.
93 print $c->request->arguments->[0];
97 Contains the url base. This will always have a trailing slash.
101 Contains the message body of the request unless Content-Type is
102 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
104 print $c->request->body
106 =item $req->content_encoding
108 Shortcut to $req->headers->content_encoding
110 =item $req->content_length
112 Shortcut to $req->headers->content_length
114 =item $req->content_type
116 Shortcut to $req->headers->content_type
120 Returns a reference to a hash containing the cookies.
122 print $c->request->cookies->{mycookie}->value;
126 Shortcut to $req->headers->header
130 Returns an L<HTTP::Headers> object containing the headers.
132 print $c->request->headers->header('X-Catalyst');
136 Contains the hostname of the remote user.
138 print $c->request->hostname
142 Shortcut for $req->body.
146 This contains be the matching part of a regexp action. otherwise it
147 returns the same as 'action'.
149 print $c->request->match;
153 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
155 print $c->request->method;
159 Get request parameters with a CGI.pm like param method.
161 $value = $c->request->param('foo');
162 @values = $c->request->param('foo');
163 @params = $c->request->param;
171 return keys %{ $self->parameters };
178 unless ( exists $self->parameters->{$param} ) {
179 return wantarray ? () : undef;
182 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
184 ? @{ $self->parameters->{$param} }
185 : $self->parameters->{$param}->[0];
189 ? ( $self->parameters->{$param} )
190 : $self->parameters->{$param};
196 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
198 next unless defined $field;
200 if ( exists $self->parameters->{$field} ) {
201 for ( $self->parameters->{$field} ) {
202 $_ = [$_] unless ref($_) eq "ARRAY";
207 $self->parameters->{$field} = $value;
215 Shortcut for $req->parameters.
217 =item $req->parameters
219 Returns a reference to a hash containing parameters. Values can
220 be either a scalar or a arrayref containing scalars.
222 print $c->request->parameters->{field};
223 print $c->request->parameters->{field}->[0];
229 print $c->request->path;
233 Contains the protocol.
237 Shortcut to $req->headers->referer. Referring page.
241 Contains a boolean whether the communciation is secure.
245 Returns a reference to an array containing regex snippets.
247 my @snippets = @{ $c->request->snippets };
251 A convenient method to $req->uploads.
253 $upload = $c->request->upload('field');
254 @uploads = $c->request->upload('field');
255 @fields = $c->request->upload;
257 for my $upload ( $c->request->upload('field') ) {
258 print $upload->filename;
267 return keys %{ $self->uploads };
274 unless ( exists $self->uploads->{$upload} ) {
275 return wantarray ? () : undef;
278 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
280 ? @{ $self->uploads->{$upload} }
281 : $self->uploads->{$upload}->[0];
285 ? ( $self->uploads->{$upload} )
286 : $self->uploads->{$upload};
292 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
294 if ( exists $self->uploads->{$field} ) {
295 for ( $self->uploads->{$field} ) {
296 $_ = [$_] unless ref($_) eq "ARRAY";
297 push( @$_, $upload );
301 $self->uploads->{$field} = $upload;
309 Returns a reference to a hash containing uploads. Values can be either a
310 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
312 my $upload = $c->request->uploads->{field};
313 my $upload = $c->request->uploads->{field}->[0];
317 Shortcut for C<< $req->base . $req->path >>.
323 my $path = shift || $self->path || '';
324 return $self->base . $path;
329 Contains the user name of user if authentication check was successful.
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.