The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Dancer2::Core::App - encapsulation of Dancer2 packages

VERSION

version 0.202000

DESCRIPTION

Everything a package that uses Dancer2 does is encapsulated into a Dancer2::Core::App instance. This class defines all that can be done in such objects.

Mainly, it will contain all the route handlers, the configuration settings and the hooks that are defined in the calling package.

Note that with Dancer2, everything that is done within a package is scoped to that package, thanks to that encapsulation.

ATTRIBUTES

plugins

runner_config

default_config

with_return

Used to cache the coderef from Return::MultiLevel within the dispatcher.

destroyed_session

We cache a destroyed session here; once this is set we must not attempt to retrieve the session from the cookie in the request. If no new session is created, this is set (with expiration) as a cookie to force the browser to expire the cookie.

METHODS

has_session

Returns true if session engine has been defined and if either a session object has been instantiated or if a session cookie was found and not subsequently invalidated.

destroy_session

Destroys the current session and ensures any subsequent session is created from scratch and not from the request session cookie

register_plugin

with_plugins( @plugin_names )

Creates instances of the given plugins and tie them to the app. The plugin classes are automatically loaded. Returns the newly created plugins.

The plugin names are expected to be without the leading Dancer2::Plugin. I.e., use Foo to mean Dancer2::Plugin::Foo.

If a given plugin is already tied to the app, the already-existing instance will be used and returned by with_plugins (think of it as using a role).

    my @plugins = $app->with_plugins( 'Foo', 'Bar' );

    # now $app uses the plugins Dancer2::Plugin::Foo
    # and Dancer2::Plugin::Bar

with_plugin( $plugin_name )

Just like with_plugin, but for a single plugin.

    my $plugin = $app->with_plugin('Foo');

redirect($destination, $status)

Sets a redirect in the response object. If $destination is not an absolute URI, then it will be made into an absolute URI, relative to the URI in the request.

halt

Flag the response object as 'halted'.

If called during request dispatch, immediately returns the response to the dispatcher and after hooks will not be run.

pass

Flag the response object as 'passed'.

If called during request dispatch, immediately returns the response to the dispatcher.

forward

Create a new request which is a clone of the current one, apart from the path location, which points instead to the new location. This is used internally to chain requests using the forward keyword.

This method takes 3 parameters: the url to forward to, followed by an optional hashref of parameters added to the current request parameters, followed by a hashref of options regarding the redirect, such as method to change the request method.

For example:

    forward '/login', { login_failed => 1 }, { method => 'GET' });

lexical_prefix

Allow for setting a lexical prefix

    $app->lexical_prefix('/blog', sub {
        ...
    });

All the route defined within the callback will have a prefix appended to the current one.

add_route

Register a new route handler.

    $app->add_route(
        method  => 'get',
        regexp  => '/somewhere',
        code    => sub { ... },
        options => $conditions,
    );

route_exists

Check if a route already exists.

    my $route = Dancer2::Core::Route->new(...);
    if ($app->route_exists($route)) {
        ...
    }

routes_regexps_for

Sugar for getting the ordered list of all registered route regexps by method.

    my $regexps = $app->routes_regexps_for( 'get' );

Returns an ArrayRef with the results.

app

Returns itself. This is simply available as a shim to help transition from a previous version in which hooks were sent a context object (originally Dancer2::Core::Context) which has since been removed.

    # before
    hook before => sub {
        my $ctx = shift;
        my $app = $ctx->app;
    };

    # after
    hook before => sub {
        my $app = shift;
    };

This meant that $app->app would fail, so this method has been provided to make it work.

    # now
    hook before => sub {
        my $WannaBeCtx = shift;
        my $app        = $WannaBeContext->app; # works
    };

AUTHOR

Dancer Core Developers

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.