[Catalyst] storing a list of items into the session

Stephen Sykes stephen at stephensykes.us
Sun Mar 23 17:44:51 GMT 2008


Pierre Moret wrote:
> Hi,
>
> I'm fairly new to Catalyst, so please excuse me if my questions are a 
> bit... hmmm... basic.
>
> My app is using the authentication and authorization features as 
> described in Jonathan Rockway's book. So far so good. Now, I want the 
> login procedure to load a list of items (I call them 'mandators') into 
> the session data so that the list can be used in every template once 
> the user has logged in. Loading one single item works well, but 
> storing the whole list doesn't. Right now, I'm quite lost...
>
> Here's my login function (from the Root.pm file):
> ---------------
> sub login : Global Form {
>     my ($self, $c) = @_;
>     my $form = $self->formbuilder;
>
>     return unless $form->submitted && $form->validate;
>
>     if ($c->login($form->field('username'),
>           $form->field('password')))
>     {
>     $c->session->{mandator} = $c->model('MotsDB::Mandator')->find();
>     $c->session->{mandators} = $c->model('MotsDB::Mandator');
>
>     $c->flash->{message} = 'Login successful.';
>     $c->res->redirect($c->uri_for('/'));
>     $c->detach();
>
>     } else {
>     $c->stash->{error} = 'Login failed.';
>     }
> }
> ---------------
>
>
> And here's the template:
>
> ---------------
> <!-- BEGIN site/right_col -->
> [% IF Catalyst.session.mandator %]
>   [% Catalyst.session.mandator.code %]<br>
>   [% Catalyst.session.mandator.name %]<br>
> [% END %]
>
> [% IF Catalyst.session.mandators %]
>   [% FOR mandator = Catalyst.session.mandators %]
>     - [% mandator.code %]: [% mandator.name %]<br>
>   [% END %]
> [% END %]
> <!-- END site/right_col -->
> ---------------
>
>
> The first part of the template correctly displays the first mandator 
> stored in the table. But the FOR loop doesn't work: it goes once 
> through, displays nothing and finishes.
>
> I'm probably missing something very basic, but I really can't figure 
> it out... Would appreciate some hints! ;-)
>
> Greets
> --Pierre
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: 
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
I'm fairly new to Catalyst myself, but I have used the following method 
to implement basically the same feature you are working on.

my (@mandator_values);
foreach my $fields_dbic (
    $c->model('MotsDB::Mandator')->search(
        undef,
        {},
    )
) {
    push @mandator_values,$fields_dbic->code,$fields_dbic->name;
}

$c->session->{mandators} = \@mandator_values;

[% IF Catalyst.session.mandators %]
  [% FOREACH mandator IN Catalyst.session.mandators %]
    - [% mandator %]: [% mandator.name %]<br>
  [% END %]
[% END %]

There's probably a better way, but this does work.

[Stephen]



More information about the Catalyst mailing list