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 if ( exists $self->parameters->{$field} ) {
199 for ( $self->parameters->{$field} ) {
200 $_ = [$_] unless ref($_) eq "ARRAY";
205 $self->parameters->{$field} = $value;
213 Shortcut for $req->parameters.
215 =item $req->parameters
217 Returns a reference to a hash containing parameters. Values can
218 be either a scalar or a arrayref containing scalars.
220 print $c->request->parameters->{field};
221 print $c->request->parameters->{field}->[0];
227 print $c->request->path;
231 Contains the protocol.
235 Shortcut to $req->headers->referer. Referring page.
239 Contains a boolean whether the communciation is secure.
243 Returns a reference to an array containing regex snippets.
245 my @snippets = @{ $c->request->snippets };
249 A convenient method to $req->uploads.
251 $upload = $c->request->upload('field');
252 @uploads = $c->request->upload('field');
253 @fields = $c->request->upload;
255 for my $upload ( $c->request->upload('field') ) {
256 print $upload->filename;
265 return keys %{ $self->uploads };
272 unless ( exists $self->uploads->{$upload} ) {
273 return wantarray ? () : undef;
276 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
278 ? @{ $self->uploads->{$upload} }
279 : $self->uploads->{$upload}->[0];
283 ? ( $self->uploads->{$upload} )
284 : $self->uploads->{$upload};
290 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
292 if ( exists $self->uploads->{$field} ) {
293 for ( $self->uploads->{$field} ) {
294 $_ = [$_] unless ref($_) eq "ARRAY";
295 push( @$_, $upload );
299 $self->uploads->{$field} = $upload;
307 Returns a reference to a hash containing uploads. Values can be either a
308 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
310 my $upload = $c->request->uploads->{field};
311 my $upload = $c->request->uploads->{field}->[0];
315 Shortcut for C<< $req->base . $req->path >>.
321 my $path = shift || $self->path || '';
322 return $self->base . $path;
327 Contains the user name of user if authentication check was successful.
329 =item $req->user_agent
331 Shortcut to $req->headers->user_agent. User Agent version string.
337 Sebastian Riedel, C<sri@cpan.org>
338 Marcus Ramberg, C<mramberg@cpan.org>
342 This program is free software, you can redistribute it and/or modify
343 it under the same terms as Perl itself.