http://www.perlmonks.org?node_id=1007638

mzedeler has asked for the wisdom of the Perl Monks concerning the following question:

I have written a somewhat large web application using Catalyst and found that I need to do some setup before the request/response-cycle starts in my direct subclass of Catalyst. The most important thing, is that I need to access the model.

I have found out that simply writing my $model = __PACKAGE__->new->model but it doesn't seem to be the right way around it, since this call seems to instantiate a complete context object with all bells and whistles. Is there any other way to do this?

Replies are listed 'Best First'.
Re: Accessing catalyst app outside request/response cycle
by Your Mother (Archbishop) on Dec 07, 2012 at 01:31 UTC

    Check the docs on Config::JFDI. You would use your app’s config to instantiate just the model. Sorry I don’t have an extended snippet just now. Google around for it if the Pod isn’t enough to kick-start you.

Re: Accessing catalyst app outside request/response cycle
by sundialsvc4 (Abbot) on Dec 06, 2012 at 22:47 UTC

    Generally, I try to design applications in such a way that there is one object/class structure which is specifically concerned with “the web application,” and these objects in turn employ another set of objects/classes which are designed to be agnostic.   You wind up looking at the task in front of you from two different levels of concern.   The first level is one that does not care how the request is being made; that is strictly concerned with the job and how to do it.   “An application,” any application web-or-not, is obliged to use them in some certain, correct, way.   The second level is strictly concerned with the flow of “web application” screens and with the steps needed to populate them.   It uses the first set of objects to do so.   (So the web-app doesn’t look as you might expect ... no database queries in the (web-app) “model” layer, for instance.   Likewise, the worker objects don’t reflect MVC.)

    The reason why I do it this way is that, no matter how hard a web-application framework strives to be “general,” it is very constrained by the strictures of the HTTP protocol.   It has a lot of baggage that has to be there, and the class-structure of any framework is unavoidably designed with the presence of that baggage in mind.   I look upon the web-app as simply a user interface.

      I get your point, but this is one of the edge cases where I am kindly asking for a shotgun and will promise to miss the best I can when I try to blow off my foot.

      The thing is that this application is maintaining an object cache which can only be updated between requests because the cache maintenance may very long time. I've managed to hook into the request cycle right after the request has been served, but at this point, the context is gone (which is a requirement because I need to be absolutely sure that nothing from the request pollutes the cache or vice versa), so how do I access the model instance?

      I tried just calling the model() class method, but it fails with a rather odd "You must pass a package name and it cannot be blessed". As far as I can see, Catalyst seems to accept that you call model() as a class method almost all the way, but at the end of the method where it for some reason tries to call ACCEPT_CONTEXT(). I don't know enough about Catalyst to claim that this is a bug, but it looks weird.

Re: Accessing catalyst app outside request/response cycle
by Anonymous Monk on Dec 06, 2012 at 20:31 UTC
    If all you want is a model object, create a model object, don't create an application object
      I must be missing something here, because I need to get access to the current model instance - not a new one.