1Dancer::Session(3) User Contributed Perl Documentation Dancer::Session(3)
2
3
4
6 Dancer::Session - session engine for the Dancer framework
7
9 version 1.3521
10
12 This module provides support for server-side sessions for the Dancer
13 web framework. The session is accessible to the user via an abstraction
14 layer implemented by the Dancer::Session class.
15
17 Configuration
18 The session engine must be first enabled in the environment settings,
19 this can be done like the following:
20
21 In the application code:
22
23 # enabling the YAML-file-based session engine
24 set session => 'YAML';
25
26 Or in config.yml or environments/$env.yml
27
28 session: "YAML"
29
30 By default sessions are disabled, you must enable them before using it.
31 If the session engine is disabled, any Dancer::Session call will throw
32 an exception.
33
34 See "Configuration" in Dancer::Session::Abstract for more configuration
35 options.
36
37 Route Handlers
38 When enabled, the session engine can be used in a route handler with
39 the keyword session. This keyword allows you to store/retrieve values
40 from the session by name.
41
42 Storing a value into the session:
43
44 session foo => 'bar';
45
46 Retrieving that value later:
47
48 my $foo = session 'foo';
49
50 You can either look for an existing item in the session storage or
51 modify one. Here is a simple example of two route handlers that
52 implement basic "/login" and "/home" actions using the session engine.
53
54 post '/login' => sub {
55 # look for params and authenticate the user
56 # ...
57 if ($user) {
58 session user_id => $user->id;
59 }
60 };
61
62 get '/home' => sub {
63 # if a user is present in the session, let him go, otherwise redirect to
64 # /login
65 if (not session('user_id')) {
66 redirect '/login';
67 }
68 };
69
70 Of course, you probably don't want to have to duplicate the code to
71 check whether the user is logged in for each route handler; there's an
72 example in the Dancer::Cookbook showing how to use a before filter to
73 check whether the user is logged in before all requests, and redirect
74 to a login page if not.
75
77 Dancer has a modular session engine that makes implementing new session
78 backends pretty easy. If you'd like to write your own, feel free to
79 take a look at Dancer::Session::Abstract.
80
81 The following engines are supported out-of-the-box (shipped with the
82 core Dancer distribution):
83
84 Dancer::Session::YAML
85 A YAML file-based session backend, pretty convenient for
86 development purposes, but maybe not the best for production needs.
87
88 Dancer::Session::Simple
89 A very simple session backend, holding all session data in memory.
90 This means that sessions are volatile, and no longer exist when the
91 process exits. This module is likely to be most useful for testing
92 purposes, and of little use for production.
93
94 Additionally, many more session engines are available from CPAN,
95 including:
96
97 Dancer::Session::Memcached
98 Session are stored in Memcached servers. This is good for
99 production matters and is a good way to use a fast, distributed
100 session storage. If you may be scaling up to add additional
101 servers later, this will be a good choice.
102
103 Dancer::Session::Cookie
104 This module implements a session engine for sessions stored
105 entirely inside encrypted cookies (this engine doesn't use a
106 server-side storage).
107
108 Dancer::Session::Storable
109 This backend stores sessions on disc using Storable, which offers
110 solid performance and reliable serialization of various data
111 structures.
112
113 Dancer::Session::MongoDB
114 A backend to store sessions using MongoDB
115
116 Dancer::Session::KiokuDB
117 A backend to store sessions using KiokuDB
118
119 Dancer::Session::PSGI
120 Let Plack::Middleware::Session handle sessions; may be useful to
121 share sessions between a Dancer app and other Plack-based apps.
122
124 Dancer::Session may depend on third-party modules, depending on the
125 session engine used. See the session engine module for details.
126
128 This module has been written by Alexis Sukrieh. See the AUTHORS file
129 that comes with this distribution for details.
130
132 This module is free software and is released under the same terms as
133 Perl itself.
134
136 See Dancer for details about the complete framework.
137
139 Dancer Core Developers
140
142 This software is copyright (c) 2010 by Alexis Sukrieh.
143
144 This is free software; you can redistribute it and/or modify it under
145 the same terms as the Perl 5 programming language system itself.
146
147
148
149perl v5.36.0 2023-02-09 Dancer::Session(3)