Managing session in before hook
Hi all, what is the right way to manage session in a before hook? I need to write a session key during the before hook execution, to retrieve it in dispatcher (to render the value). I tried many ways like: hook before => sub { my $context = shift; ... session 'foo' => 'bar'; $context->app->session->{'foo'} = 'bar'; $session = $context->session; $session->write('foo', 'bar') } but everytime nothing arrives to the dispatcher and nothing is actually written (I used Session::YAML to test this). Where am I wrong? -- Cymon Coniglio domina, http://www.therabbit.it
On Tue, Jun 18, 2013 at 6:12 PM, Cymon <cymon.ML@gmail.com> wrote:
what is the right way to manage session in a before hook?
You need to give us more details and sample code. Where is your before hook? In your application? Or in a plugin? What are you doing in your dispatcher? FWIW, sessions aren't flushed (e.g. to disk) until an after hook, so when you say "nothing is written", when are you checking? David -- David Golden <xdg@xdg.me> Take back your inbox! → http://www.bunchmail.com/ Twitter/IRC: @xdg
Il giorno Tue, 18 Jun 2013 19:15:37 -0400 David Golden <xdg@xdg.me> ha scritto:
On Tue, Jun 18, 2013 at 6:12 PM, Cymon <cymon.ML@gmail.com> wrote:
what is the right way to manage session in a before hook?
You need to give us more details and sample code. Where is your before hook? In your application? Or in a plugin? What are you doing in your dispatcher?
FWIW, sessions aren't flushed (e.g. to disk) until an after hook, so when you say "nothing is written", when are you checking?
David
-- David Golden <xdg@xdg.me> Take back your inbox! → http://www.bunchmail.com/ Twitter/IRC: @xdg _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
I give you a little not working example. In the main package of the Dancer2 project I have: package project; use Dancer2 ':syntax'; use Dancer2::Template::TemplateToolkit; hook before => sub { my $context = shift; my $newpath = '/there'; //this or any other of the ones I said $context->app->session->{'foo'} = 'bar'; //I tried also to turn off forwording with no effect $context->response( forward($newpath )); $context->response->halt; }; then in the dispatcher: get '/there' => sub { print session('foo'); //nothing in it template 'index'; }; I checked the YAML file using Session::YAML after navigation. They're all empty. -- Cymon Coniglio domina, http://www.therabbit.it
I'm not sure what exactly is the problem, but I'm perplexed why you're importing only ":syntax" -- that means your code isn't setting up a dancer app properly. And you're calling forward from a hook and I think that might only valid from a request handler. Here's a simple example that works: https://gist.github.com/dagolden/5810631 If you run that and go to the localhost URL, you should see "bar" (as set in the session). I hope that helps you get on the right track. David On Tue, Jun 18, 2013 at 7:30 PM, Cymon <cymon.ML@gmail.com> wrote:
Il giorno Tue, 18 Jun 2013 19:15:37 -0400 David Golden <xdg@xdg.me> ha scritto:
On Tue, Jun 18, 2013 at 6:12 PM, Cymon <cymon.ML@gmail.com> wrote:
what is the right way to manage session in a before hook?
You need to give us more details and sample code. Where is your before hook? In your application? Or in a plugin? What are you doing in your dispatcher?
FWIW, sessions aren't flushed (e.g. to disk) until an after hook, so when you say "nothing is written", when are you checking?
David
-- David Golden <xdg@xdg.me> Take back your inbox! → http://www.bunchmail.com/ Twitter/IRC: @xdg _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
I give you a little not working example. In the main package of the Dancer2 project I have:
package project;
use Dancer2 ':syntax'; use Dancer2::Template::TemplateToolkit;
hook before => sub { my $context = shift; my $newpath = '/there'; //this or any other of the ones I said $context->app->session->{'foo'} = 'bar';
//I tried also to turn off forwording with no effect $context->response( forward($newpath )); $context->response->halt; };
then in the dispatcher:
get '/there' => sub { print session('foo'); //nothing in it template 'index'; };
I checked the YAML file using Session::YAML after navigation. They're all empty.
-- Cymon Coniglio domina, http://www.therabbit.it _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- David Golden <xdg@xdg.me> Take back your inbox! → http://www.bunchmail.com/ Twitter/IRC: @xdg
Il giorno Tue, 18 Jun 2013 20:06:47 -0400 David Golden <xdg@xdg.me> ha scritto: Hi, sorry for answering you so late, but work took priority :-)
I'm not sure what exactly is the problem, but I'm perplexed why you're importing only ":syntax" -- that means your code isn't setting up a dancer app properly.
That code was generated using the dancer2 -a MYAPP script, with just "Use Dancer2" in the bin/app.pl script and Dancer :syntax in the lib. Studying Dancer2 a bit more, now I think that every lib file should have its own "Use Dancer2" to correctly set up an App... am I right?
And you're calling forward from a hook and I think that might only valid from a request handler.
I found that way for forwarding using before hook in a Dancer2 test: https://github.com/PerlDancer/Dancer2/blob/devel/t/path_info.t It works, but i think that it doesn't flush the session so session changes are ineffective. So my question now is: how can I communicate data to the forwarded path from the one doing the forward? Thanks for all, Simone
Here's a simple example that works:
https://gist.github.com/dagolden/5810631
If you run that and go to the localhost URL, you should see "bar" (as set in the session).
I hope that helps you get on the right track.
David
On Tue, Jun 18, 2013 at 7:30 PM, Cymon <cymon.ML@gmail.com> wrote:
Il giorno Tue, 18 Jun 2013 19:15:37 -0400 David Golden <xdg@xdg.me> ha scritto:
On Tue, Jun 18, 2013 at 6:12 PM, Cymon <cymon.ML@gmail.com> wrote:
what is the right way to manage session in a before hook?
You need to give us more details and sample code. Where is your before hook? In your application? Or in a plugin? What are you doing in your dispatcher?
FWIW, sessions aren't flushed (e.g. to disk) until an after hook, so when you say "nothing is written", when are you checking?
David
-- David Golden <xdg@xdg.me> Take back your inbox! → http://www.bunchmail.com/ Twitter/IRC: @xdg _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
I give you a little not working example. In the main package of the Dancer2 project I have:
package project;
use Dancer2 ':syntax'; use Dancer2::Template::TemplateToolkit;
hook before => sub { my $context = shift; my $newpath = '/there'; //this or any other of the ones I said $context->app->session->{'foo'} = 'bar';
//I tried also to turn off forwording with no effect $context->response( forward($newpath )); $context->response->halt; };
then in the dispatcher:
get '/there' => sub { print session('foo'); //nothing in it template 'index'; };
I checked the YAML file using Session::YAML after navigation. They're all empty.
-- Cymon Coniglio domina, http://www.therabbit.it _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Cymon Coniglio domina, http://www.therabbit.it
participants (2)
-
Cymon -
David Golden