1Dancer2::Core::App(3) User Contributed Perl DocumentationDancer2::Core::App(3)
2
3
4
6 Dancer2::Core::App - encapsulation of Dancer2 packages
7
9 version 0.207000
10
12 Everything a package that uses Dancer2 does is encapsulated into a
13 "Dancer2::Core::App" instance. This class defines all that can be done
14 in such objects.
15
16 Mainly, it will contain all the route handlers, the configuration
17 settings and the hooks that are defined in the calling package.
18
19 Note that with Dancer2, everything that is done within a package is
20 scoped to that package, thanks to that encapsulation.
21
23 plugins
24 runner_config
25 default_config
26 with_return
27 Used to cache the coderef from Return::MultiLevel within the
28 dispatcher.
29
30 destroyed_session
31 We cache a destroyed session here; once this is set we must not attempt
32 to retrieve the session from the cookie in the request. If no new
33 session is created, this is set (with expiration) as a cookie to force
34 the browser to expire the cookie.
35
37 has_session
38 Returns true if session engine has been defined and if either a session
39 object has been instantiated or if a session cookie was found and not
40 subsequently invalidated.
41
42 change_session_id
43 Changes the session ID used by the current session. This should be used
44 on any change of privilege level, for example on login. Returns the new
45 session ID.
46
47 destroy_session
48 Destroys the current session and ensures any subsequent session is
49 created from scratch and not from the request session cookie
50
51 register_plugin
52 with_plugins( @plugin_names )
53 Creates instances of the given plugins and tie them to the app. The
54 plugin classes are automatically loaded. Returns the newly created
55 plugins.
56
57 The plugin names are expected to be without the leading
58 "Dancer2::Plugin". I.e., use "Foo" to mean "Dancer2::Plugin::Foo".
59
60 If a given plugin is already tied to the app, the already-existing
61 instance will be used and returned by "with_plugins" (think of it as
62 using a role).
63
64 my @plugins = $app->with_plugins( 'Foo', 'Bar' );
65
66 # now $app uses the plugins Dancer2::Plugin::Foo
67 # and Dancer2::Plugin::Bar
68
69 with_plugin( $plugin_name )
70 Just like "with_plugin", but for a single plugin.
71
72 my $plugin = $app->with_plugin('Foo');
73
74 redirect($destination, $status)
75 Sets a redirect in the response object. If $destination is not an
76 absolute URI, then it will be made into an absolute URI, relative to
77 the URI in the request.
78
79 halt
80 Flag the response object as 'halted'.
81
82 If called during request dispatch, immediately returns the response to
83 the dispatcher and after hooks will not be run.
84
85 pass
86 Flag the response object as 'passed'.
87
88 If called during request dispatch, immediately returns the response to
89 the dispatcher.
90
91 forward
92 Create a new request which is a clone of the current one, apart from
93 the path location, which points instead to the new location. This is
94 used internally to chain requests using the forward keyword.
95
96 This method takes 3 parameters: the url to forward to, followed by an
97 optional hashref of parameters added to the current request parameters,
98 followed by a hashref of options regarding the redirect, such as
99 "method" to change the request method.
100
101 For example:
102
103 forward '/login', { login_failed => 1 }, { method => 'GET' });
104
105 lexical_prefix
106 Allow for setting a lexical prefix
107
108 $app->lexical_prefix('/blog', sub {
109 ...
110 });
111
112 All the route defined within the callback will have a prefix appended
113 to the current one.
114
115 add_route
116 Register a new route handler.
117
118 $app->add_route(
119 method => 'get',
120 regexp => '/somewhere',
121 code => sub { ... },
122 options => $conditions,
123 );
124
125 route_exists
126 Check if a route already exists.
127
128 my $route = Dancer2::Core::Route->new(...);
129 if ($app->route_exists($route)) {
130 ...
131 }
132
133 routes_regexps_for
134 Sugar for getting the ordered list of all registered route regexps by
135 method.
136
137 my $regexps = $app->routes_regexps_for( 'get' );
138
139 Returns an ArrayRef with the results.
140
141 app
142 Returns itself. This is simply available as a shim to help transition
143 from a previous version in which hooks were sent a context object
144 (originally "Dancer2::Core::Context") which has since been removed.
145
146 # before
147 hook before => sub {
148 my $ctx = shift;
149 my $app = $ctx->app;
150 };
151
152 # after
153 hook before => sub {
154 my $app = shift;
155 };
156
157 This meant that "$app->app" would fail, so this method has been
158 provided to make it work.
159
160 # now
161 hook before => sub {
162 my $WannaBeCtx = shift;
163 my $app = $WannaBeContext->app; # works
164 };
165
166 $SIG{__DIE__} Compatibility via $Dancer2::Core::App::EVAL_SHIM
167 If an installation wishes to use $SIG{__DIE__} hooks to enhance their
168 error handling then it may be required to ensure that certain
169 bookkeeping code is executed within every "eval BLOCK" that Dancer2
170 performs. This can be accomplished by overriding the global variable
171 $Dancer2::Core::App::EVAL_SHIM with a subroutine which does whatever
172 logic is required.
173
174 This routine must perform the equivalent of the following subroutine:
175
176 our $EVAL_SHIM = sub {
177 my $code = shift;
178 return $code->(@_);
179 };
180
181 An example of overriding this sub might be as follows:
182
183 $Dancer2::Core::App::EVAL_SHIM = sub {
184 my $code = shift;
185 local $IGNORE_EVAL_COUNTER = $IGNORE_EVAL_COUNTER + 1;
186 return $code->(@_);
187 };
188
189 Note: that this is a GLOBAL setting, which must be set up before any
190 form of dispatch or use of Dancer2.
191
193 Dancer Core Developers
194
196 This software is copyright (c) 2018 by Alexis Sukrieh.
197
198 This is free software; you can redistribute it and/or modify it under
199 the same terms as the Perl 5 programming language system itself.
200
201
202
203perl v5.28.1 2018-11-14 Dancer2::Core::App(3)