Dancer2::Core::App(3p)

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

VERSION
version 1.1.2

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 that will return from back to the dispatcher, across an arbitrary number of stack frames.

  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.

  change_session_id
Changes the session ID used by the current session. This should be used on any change of privilege level, for example on login. Returns the new session ID.

  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');

  add_route
Register a new route handler.

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

Returns a new Dancer2::Core::Route object created with the passed arguments.

  route_exists
Returns a true value if a route already exists, otherwise false.

    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.

  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' });

  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
    };

  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.

  $SIG{__DIE__} Compatibility via $Dancer2::Core::App::EVAL_SHIM
If an installation wishes to use $SIG{__DIE__} hooks to enhance their error handling then it may be required to ensure that certain bookkeeping code is executed within every "eval BLOCK" that Dancer2 performs. This can be accomplished by overriding the global variable $Dancer2::Core::App::EVAL_SHIM with a subroutine which does whatever logic is required.

This routine must perform the equivalent of the following subroutine:

    our $EVAL_SHIM = sub {
        my $code = shift;
        return $code->(@_);
    };

An example of overriding this sub might be as follows:

    $Dancer2::Core::App::EVAL_SHIM = sub {
        my $code = shift;
        local $IGNORE_EVAL_COUNTER = $IGNORE_EVAL_COUNTER + 1;
        return $code->(@_);
    };

Note: that this is a GLOBAL setting, which must be set up before any form of dispatch or use of Dancer2.

AUTHOR
Dancer Core Developers

COPYRIGHT AND LICENSE
This software is copyright (c) 2024 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.