Wednesday 15 April 2015

perl - How to initialize a session in Catalyst application? -



perl - How to initialize a session in Catalyst application? -

here myapp's module lib folder:

package myapp; utilize moose; utilize namespace::autoclean; utilize catalyst::runtime 5.80; utilize catalyst qw/ configloader session session::store session::state static::simple /; extends 'catalyst'; our $version = '0.01'; __package__->config( name => 'myapp', # disable deprecated behavior needed old applications disable_component_resolution_regex_fallback => 1, enable_catalyst_header => 1, # send x-catalyst header ); sub init { ( $c ) = @_; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); $c->session->{ed_year} = $year + 1900; } # start application __package__->setup(); __package__->init(); 1;

the illustration above wrong, there no available context($c). know if possible initialize session in catalyst application right in main module. here initialize global variables, used later views, models , controllers?

best regards, sk

a session associated user, , artifact of interaction user. can't create session in main programme - rightly say, there's no context @ point. in case, think you're wanting configure variables available user of application, they're global, not user-specific anyway.

use __package__->config - it's hashref, , that's it's for. you're not limited documented keys.

for example:

__package__->config( name => 'myapp', # disable deprecated behavior needed old applications disable_component_resolution_regex_fallback => 1, enable_catalyst_header => 1, # send x-catalyst header ed_year => (localtime())[5] + 1900, foo => { bar => 1, baz => 'quux' }, );

in models, views , controllers values available $c->config->{ed_year} , $c->config->{foo}->{baz} , on.

by way, perhaps utilize of ed_year simplistic example, consider how instantiated: date , time server started, not time of current request. if latter want, set in auto handler of root.pm controller. , don't roll own localtime, utilize datetime module.

perl catalyst

No comments:

Post a Comment