1 package Catalyst::Response;
6 with 'MooseX::Emulate::Class::Accessor::Fast';
8 has cookies => (is => 'rw', default => sub { {} });
9 has body => (is => 'rw', default => undef);
10 sub has_body { defined($_[0]->body) }
12 has location => (is => 'rw');
13 has status => (is => 'rw', default => 200);
14 has finalized_headers => (is => 'rw', default => 0);
17 isa => 'HTTP::Headers',
18 handles => [qw(content_encoding content_length content_type header)],
19 default => sub { HTTP::Headers->new() },
27 clearer => '_clear_context',
30 sub output { shift->body(@_) }
32 sub code { shift->status(@_) }
38 Catalyst::Response - stores output responding to the current client request
45 $res->content_encoding;
58 This is the Catalyst Response class, which provides methods for responding to
59 the current client request. The appropriate L<Catalyst::Engine> for your environment
60 will turn the Catalyst::Response into a HTTP Response and return it to the client.
64 =head2 $res->body( $text | $fh | $iohandle_object )
66 $c->response->body('Catalyst rocks!');
68 Sets or returns the output (text or binary data). If you are returning a large body,
69 you might want to use a L<IO::Handle> type of object (Something that implements the read method
70 in the same fashion), or a filehandle GLOB. Catalyst
71 will write it piece by piece into the response.
75 Predicate which returns true when a body has been set.
79 Alias for $res->status.
81 =head2 $res->content_encoding
83 Shortcut for $res->headers->content_encoding.
85 =head2 $res->content_length
87 Shortcut for $res->headers->content_length.
89 =head2 $res->content_type
91 Shortcut for $res->headers->content_type.
93 This value is typically set by your view or plugin. For example,
94 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
95 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
99 Returns a reference to a hash containing cookies to be set. The keys of the
100 hash are the cookies' names, and their corresponding values are hash
101 references used to construct a L<CGI::Simple::Cookie> object.
103 $c->response->cookies->{foo} = { value => '123' };
105 The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
106 parameters of the same name, except they are used without a leading dash.
107 Possible parameters are:
127 Shortcut for $res->headers->header.
131 Returns an L<HTTP::Headers> object, which can be used to set headers.
133 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
137 Alias for $res->body.
139 =head2 $res->redirect( $url, $status )
141 Causes the response to redirect to the specified URL. The default status is
144 $c->response->redirect( 'http://slashdot.org' );
145 $c->response->redirect( 'http://slashdot.org', 307 );
147 This is a convenience method that sets the Location header to the
148 redirect destination, and then sets the response status. You will
149 want to C< return > or C<< $c->detach() >> to interrupt the normal
150 processing flow if you want the redirect to occur straight away.
152 B<Note:> do not give a relative URL as $url, i.e: one that is not fully
153 qualified (= C<http://...>, etc.) or that starts with a slash
154 (= C</path/here>). While it may work, it is not guaranteed to do the right
155 thing and is not a standard behaviour. You may opt to use uri_for() or
156 uri_for_action() instead.
164 my $location = shift;
165 my $status = shift || 302;
167 $self->location($location);
168 $self->status($status);
171 return $self->location;
174 =head2 $res->location
176 Sets or returns the HTTP 'Location'.
180 Sets or returns the HTTP status.
182 $c->response->status(404);
184 $res->code is an alias for this, to match HTTP::Response->code.
186 =head2 $res->write( $data )
188 Writes $data to the output stream.
194 =head2 $res->print( @data )
196 Prints @data to the output stream, separated by $,. This lets you pass
197 the response object to functions that want to write to an L<IO::Handle>.
205 defined $self->write($data) or return;
208 defined $self->write($,) or return;
209 defined $self->write($_) or return;
211 defined $self->write($\) or return;
218 Catalyst Contributors, see Catalyst.pm
222 This library is free software. You can redistribute it and/or modify
223 it under the same terms as Perl itself.
227 __PACKAGE__->meta->make_immutable;