NAME
PlackX::Framework::Response - Subclass of Plack::Response for PlackX::Framework
CLASS METHODS
- new()
-
Returns a new object. This is done for you by the framework.
- GlobalResponse()
-
If your app's subclass of PlackX::Framework::Handler's use_global_request_response method returns a true value, PlackX::Framework will set up a global response object for you, which can be retrieved via this class method.
This feature is turned off by default to avoid action-at-a-distance bugs. It is preferred to use the request object instance passed to each route's subroutine.
OBJECT METHODS
- charset(), charset($newval)
-
Get or set the charset portion of the content-type header.
- content_type(), content_type($newval)
-
Like Plack::Response, this is a shortcut for HTTP::Headers::Fast->content_type; however, if a charset has been set with the charset() method, it will add the charset to the content-type header, if no charset is specified in $newval.
- flash(value)
-
Sets the flash cookie to the value specified, or clears it if the value is false. PXF automatically clears the cookie on the subsequent request, unless you set a different one.
- flash_redirect(value, url)
-
Combines flash(value) and redirect(url) with a 303 (SEE OTHER) response code.
- next()
-
Syntactic sugar for returning a false value. Indicates to PlackX::Framework to execute the next matching filter.
return $response->next; # equivalent to return;See also the stop() method below.
- no_cache(BOOL)
-
If passed a true value, adds HTTP Pragma and Cache-Control headers to "no-cache". If passed a false value, sets these headers to empty string.
- print($string), print(@strings)
-
Adds $string or @strings to the response body, or write them to the PSGI output stream if streaming mode has been activated (see the render_stream() and stream() methods below).
When streaming is activated, you should append your print strings with newlines to encourage the server (and browser) to flush the buffer.
Unfortunately, the PSGI specification does not provide a way to flush the buffer, but if you are using a server that allows this, perhaps you could do:
$response->print($string); $response->stream_writer->flush_buffer();Or override print in your subclass:
package MyApp::Response { use parent 'PlackX::Framework::Response'; sub print ($self, @lines) { $self->SUPER::print(@lines); $self->stream_writer->flush_buffer() if $self->stream_writer; } } - redirect($url, $http_status)
-
Like Plack::Response->redirect, except the default http status is 303 See Other instead of 302 Found. This matches the more common type of redirect in a web app, which is directing the user to another page after a prevous request was processed (such as a log in form).
- render($key => @values)
-
An alias for $obj->render_$key(@values). For example, instead of calling render_html(...), you could call render(html => ...). Used by PXF's Router module to implement shortcuts to the appropriate $response->render_*() method.
- render_html($string)
-
Sets the content-type to text/html and sets the response body to $string.
- render_json($ref)
-
Sets the content-type to application/json and encodes $ref to JSON, setting the response body to the resulting string.
- render_stream(CODE)
-
Call stream() with CODE and return the response object.
Example:
route '/stream-example' => sub ($request, $response) { $response->print(...); # html header return $response->render_stream(sub { # Do some slow actions $response->print(...); # Do more slow actions }); }; - render_template(@args)
-
Shortcut for $obj->template->render(@ags)
- render_text($string)
-
Sets the content-type to text/plain and sets the response body to $string.
- set_defaults()
-
Set defaults for the response object. This method is called automatically by PlackX::Framework::Handler.
- stash(), stash($hashref)
-
Returns the current stash hashref, optionally setting it to a new one.
- stream(CODE)
-
Get or set a code reference for PSGI streaming. It is recommended you simply return render_stream(CODE) as described above, but you can use this directly like the below example.
Example:
route '/stream-example' => sub ($request, $response) { $response->stream(sub { $response->print('I am in a stream!'); }); # Anything you do here will be executed BEFORE the stream code! $response->print('I am before the stream!'); return $response; }; - stop()
-
Syntactic sugar for returning the object itself. Indicates to PlackX::Framework that it should render the response. Useful for semantics in filter actions. The default router engine does not support multiple matching routes, so not so useful in route actions. See also the equivalent inverse method, next().
# In a filter return $response->stop; # equivalent to return $response; - template()
-
Returns the PlackX::Framework::Template object, or undef if templating has not been set up.
META
For author, copyright, and license, see PlackX::Framework.