[Catalyst] Creating my own controller and another question

John Napiorkowski jjn1056 at yahoo.com
Fri Apr 28 15:29:31 CEST 2006


Hi,

I'm creating a class that subclasses from
Catalyst::Controller for use to encapsulate some
functionality for certain pages on my site.  I realize
that this could be potentially done with with a
plugin, but for this purpose I feel subclassing
Catalyst::Controller is the cleanest and most
maintainable approach.

The problem I am having is that I want my controller
to prepare some data when it is first loaded.  The
function is to create a list of objects that will
later be retrieved, so I can't just put it into the
config.  However I can't figure out the best why (or
the best practices way) to do that.  Should I just
write my own new subroutine to do this?

I've seen that I could use a sub begin: Private for
this but that get's called each time the action is
invoked and I'd like to avoid that overhead.  That's
what I am currently doing until I find out about a
better solution.



Another problem.  There is some strange things going
on when I have a config in a base controller.  For
example I have a class "element" that inherits from
"Catalyst::Controller" which has the following line
(again this is contrived a bit for clarity):

package element;

use strict;
use warnings;

use base qw/Catalyst::Controller/;

__PACKAGE__->config( view => 'element');

sub view
{
  $self = shift;
  return $self->{view};
}

Then I have two classes that inherit from this element
class:

package page;

use base qw/element/;

__PACKAGE__->config( view => 'page');


and

package portlet;

use base qw/element/;

__PACKAGE__->config( view => 'portlet');

So all three classes have a method called "view". 
However this method doesn't do what I would think it
would.  If I subclass any of these three classes for a
catalyst controller and call $self->view on that
subclass I always get 'portlet' as a response, instead
of what was defined in the config for the local class.
 Maybe I just don't understand how this is supposed to
work?

Oh, one last quick thing: Is there an easy way to get
the context object if you only have the self object,
like in a controller where you have make some local
accessors?  This is causing me a lot of trouble.
Any thoughts or questions from the community would be
appreciated.

--john napiorkowski

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Catalyst mailing list