NAME

Prancer::Context

SYNOPSIS

The context gives you access to all pieces of a request from request parameters to cookies, sessions and headers. It can be used by calling context from anywhere in a package that extends Prancer::Application. Otherwise you must pass it to other packages to make it available.

use Prancer::Application qw(:all);
use parent qw(Prancer::Application);

sub handle {
    my ($self, $env) = @_;

    mount('GET', '/', sub {
        context->header(set => 'Content-Type', value => 'text/plain');
        context->body("hello world");
        context->finalize(200);
    });

    return dispatch;
}

METHODS

env

Returns the PSGI environment for the request.

template

This is a magical wrapper to Prancer::template() in that it works by calling Prancer::template but it will merge params, config, and session into the list of template variables using their respective names as the first key. This allows you to write this in your template:

<% params.bar %>
<% config.foo.bar %>
<% session.asdf.fdsa %>
session

This gives access to the session in various ways. For example:

my $does_foo_exist = context->session->has('foo');
my $foo = context->session->get('foo');
my $bar = context->session->get('bar', 'some default value if bar does not exist');
context->session->set('foo', 'bar');
context->session->remove('foo');

Changes made to the session are persisted immediately to whatever medium backs your sessions.

request

Returns the Prancer::Request object for the request.

response

Returns the Prancer::Response object that will be used to generate the response.

This gives access request and response headers. For example:

# get a request header
my $useragent = context->header(get => 'user-agent');

# set a response header
context->header(set => 'Content-Type', value => 'text/plain');

This gives access to request and response cookies. For example:

# get a request cookie
my $foo = context->cookie(get => 'foo');

# set a response cookie
context->cookie(set => 'foo', value => {
    'value' => 'bar',
    'domain' => '.example.com',
});
param

This is a wrapper around the param method to Prancer::Request.

params

This is a wrapper around the params method to Prancer::Request.

upload

This is a wrapper around the upload method to Prancer::Request.

uploads

This is a wrapper around the uploads method to Prancer::Request.

body

This is a wrapper around the body method to Prancer::Response.

finalize

This is a wrapper around the finalize method to Prancer::Response.