[Catalyst] access stash outside methods

John Napiorkowski jjn1056 at yahoo.com
Tue Aug 21 16:39:02 GMT 2007

--- Aditya Verma <verma.aditya1 at gmail.com> wrote:

> Hi,
> I am running catalyst under apache configured with
> mod_perl.
> Can I somehow access stash out methods.
> I want to declare a global variable inside
> controller and outside methods
> which will be accessible in all controllers.
> stash could be a solution but outside methods
> context object is not
> available so i don't have access to stash.
> please help me solve the problem.
> Thanks,
> Aditya


I may not fully understand your use case, but
typically the stash is used for a particular context,
not as a global thing.

You can alway pass the context to methods that need
it.  I know it seems cumbersome, but the reason is to
make you really think if you need it, the idea being
that you should try to decouple your context as much
as possible.

If you need some global information there are a few
ways I can think of:

1) put it into the configuration file.  This is great
for static stuff, like admin email address, etc.
2) Create a model that provides the information you
need.  If this information is based on the current
context you can inherit from
"Catalyst::Component::ACCEPT_CONTEXT" to build it.
3) Create a base controller with a method that gets
the information you need.  The have the controllers
that need this information inherit from that.

You might get the suggestion to make this a catalyst
plugin, but that's no longer recommended best
practices.  Plugins should be reserved for when you
are trulying enhancing/extending the basic Catalyst

Please share with us the details of the use case and
maybe we can help.


Choose the right car based on your needs.  Check out Yahoo! Autos new Car Finder tool.

More information about the Catalyst mailing list