The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Slick::Context

SYNOPSIS

Slick::Context is an Moo wrapper around the HTTP lifecycle. It encompases a Plack::Request and a bunch of other helpers to make it easy to handle HTTP in Slick.

API

content

    $s->post('/foo', sub {
        my ($app, $context) = @_;
        my $data = $context->content;
    });

Decodes the body of the Plack::Request via the following:

body

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->body('Foo!');
    });

Sets the body of the response to whatever is provided. Note: You'll probably want to use "html", "json", "text" or "yaml" instead of this.

html

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->body('<h1>Foo!</h1>');
    });

Sets the body of the response and sets the Content-Type header to text/html. Returns the context.

Note, you should have use utf8; enabled.

json

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->json({ hello => 'world' });
    });

Sets the body of the response and sets the Content-Type header to application/json. Returns the context.

Note, you should have use utf8; enabled.

yaml

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->yaml([ { hello => 'world' });
    });

Sets the body of the response and sets the Content-Type header to application/yaml. Returns the context.

Note, you should have use utf8; enabled.

text

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->text("Hello World!");
    });

Sets the body of the response and sets the Content-Type header to text/plain. Returns the context.

Note, you should have use utf8; enabled.

to_psgi

Converts the Slick::Context to Plack response. In the format:

    [$status, [ @headers ], [ @body ]]

status

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->status(201);
    });

Sets the status code of the response, returns the context.

redirect

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->redirect("/foo");
    });

Sets the response to redirect to a given location, optionally provide another status code as a second argument if you don't want 303. Returns the context object.

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->header(Foo => 'bar');
    });

Sets a header on the underlying response HashRef. Returns the context.

response

Returns the response HashRef that will be used to create the PSGI response via "to_psgi".

query

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->query('bar');
    });

Returns the value of a specified query parameter key, returns undef if there is no such key.

See "queries" for the raw query parameter HashRef.

queries

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->queries->{'bar'};
    });

Returns the query parameters as a HashRef.

param

    $s->get('/foo', sub {
        my ($app, $context) = @_;
        $context->param('bar');
    });

Returns the value of a specified path parameter key, returns undef if there is no such key.

See "params" for the raw path parameter HashRef.

params

    $s->get('/foo/{bar}', sub {
        my ($app, $context) = @_;
        $context->params->{'bar'};
    });

Returns the path parameters as a HashRef.

stash

Returns a transient HashRef that is persistent per request, this is for inter-layer communication.

id

Returns an arbitrary tracing ID in the form of a 4-digit number.

See also

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 412:

Unknown directive: =over2

Around line 414:

'=item' outside of any '=over'