1 package Catalyst::Request;
4 use base 'Class::Accessor::Fast';
6 __PACKAGE__->mk_accessors(
7 qw/action address arguments base cookies headers hostname match method
8 parameters path snippets uploads/
12 *params = \¶meters;
14 sub content_encoding { shift->headers->content_encoding(@_) }
15 sub content_length { shift->headers->content_length(@_) }
16 sub content_type { shift->headers->content_type(@_) }
17 sub header { shift->headers->header(@_) }
18 sub referer { shift->headers->referer(@_) }
19 sub user_agent { shift->headers->user_agent(@_) }
22 my ( $self, $map, $values ) = @_;
24 while ( my ( $name, $value ) = splice( @{$values}, 0, 2 ) ) {
26 if ( exists $map->{$name} ) {
27 for ( $map->{$name} ) {
28 $_ = [$_] unless ref($_) eq "ARRAY";
33 $map->{$name} = $value;
40 Catalyst::Request - Catalyst Request Class
51 $req->content_encoding;
74 This is the Catalyst Request class, which provides a set of accessors to the
75 request data. The request object is prepared by the specialized Catalyst
76 Engine module thus hiding the details of the particular engine implementation.
85 Contains the requested action.
87 print $c->request->action;
91 Contains the remote address.
93 print $c->request->address
97 Shortcut for arguments
101 Returns a reference to an array containing the arguments.
103 print $c->request->arguments->[0];
107 Contains the url base. This will always have a trailing slash.
109 =item $req->content_encoding
111 Shortcut to $req->headers->content_encoding
113 =item $req->content_length
115 Shortcut to $req->headers->content_length
117 =item $req->content_type
119 Shortcut to $req->headers->content_type
123 Returns a reference to a hash containing the cookies.
125 print $c->request->cookies->{mycookie}->value;
129 Shortcut to $req->headers->header
133 Returns an L<HTTP::Headers> object containing the headers.
135 print $c->request->headers->header('X-Catalyst');
139 Contains the hostname of the remote user.
141 print $c->request->hostname
145 This contains be the matching part of a regexp action. otherwise it
146 returns the same as 'action'.
148 print $c->request->match;
152 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
154 print $c->request->method;
158 Get request parameters with a CGI.pm like param method.
160 $value = $c->request->param('foo');
161 @values = $c->request->param('foo');
162 @params = $c->request->param;
170 return keys %{ $self->parameters };
175 unless ( exists $self->parameters->{$param} ) {
176 return wantarray ? () : undef;
179 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
181 ? @{ $self->parameters->{$param} }
182 : $self->parameters->{$param}->[0];
186 ? ( $self->parameters->{$param} )
187 : $self->parameters->{$param};
193 Shortcut for $req->parameters.
195 =item $req->parameters
197 Returns a reference to a hash containing parameters. Values can
198 be either a scalar or a arrayref containing scalars.
200 print $c->request->parameters->{field};
201 print $c->request->parameters->{field}->[0];
207 print $c->request->path;
211 Shortcut to $req->headers->referer. Referring page.
215 Returns a reference to an array containing regex snippets.
217 my @snippets = @{ $c->request->snippets };
221 A convenient method to $req->uploads.
223 $upload = $c->request->upload('field');
224 @uploads = $c->request->upload('field');
225 @fields = $c->request->upload;
227 for my $upload ( $c->request->upload('field') ) {
228 print $upload->filename;
237 return keys %{ $self->uploads };
242 unless ( exists $self->uploads->{$upload} ) {
243 return wantarray ? () : undef;
246 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
248 ? @{ $self->uploads->{$upload} }
249 : $self->uploads->{$upload}->[0];
253 ? ( $self->uploads->{$upload} )
254 : $self->uploads->{$upload};
260 Returns a reference to a hash containing uploads. Values can be either a
261 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
263 my $upload = $c->request->uploads->{field};
264 my $upload = $c->request->uploads->{field}->[0];
266 =item $req->user_agent
268 Shortcut to $req->headers->user_agent. User Agent version string.
274 Sebastian Riedel, C<sri@cpan.org>
275 Marcus Ramberg, C<mramberg@cpan.org>
279 This program is free software, you can redistribute it and/or modify
280 it under the same terms as Perl itself.