[Catalyst] Accessing a Controller from ~/script
Tomas Doran
bobtfish at bobtfish.net
Wed Mar 11 16:08:23 GMT 2009
Dermot wrote:
> Currently I load the configuration into the stand alone Class with a
> combination of FindBin and Config::General. I thought I could avoid
> that seeming as config is already in my context ($c). So I did:
>
> package MyApp::Model::MyAdaptor;
> use strict;
> use warnings;
> use base 'Catalyst::Model::Adaptor';
> __PACKAGE__->config( class => 'MyImporter');
> __PACKAGE__->mk_accessors(qw/config/);
> sub ACCEPT_CONTEXT {
> my ($self, $c ) = @_;
> $self = bless({ %$self,
> config => $c->config(''),
> }, ref($self));
> return $self;
> }
> 1;
>
> but this is giving me an error:
>
> Couldn't instantiate component "MyApp::Model::MyAdaptor", "Can't use
> string ("MyApp::Model::MyAdaptor") as a HASH ref while "strict refs"
> in use at /usr/lib/perl5/site_perl/5.8.6/Class/Accessor/Fast.pm line
> 38."
>
>
Defining an ACCEPT_CONTEXT method and using Catalyst::Model::Adaptor at
the same time makes no sense.
I'm not sure how you think adding magic handwaves (i.e. ACCEPT_CONTEXT)
is going to solve your problem.
Let me restate your issue for you (as I understand it):
. I have a Catalyst app, with config in a config file
. I want to use the models externally
. Writing code to read the config in myself, and then instantiate the
model seems like a royal pain in the ass, there is an easier way, right?
The answer is 'no, there isn't, but there should be'. Config loading
should be made more generic such that you could easily write:
use CatalystX::Model::OutsideCatalyst qw/get_model_from_app/;
my $model = get_component_from_app('/path/to/MyApp', 'Foo');
and then deal with $model, already correctly configured, without having
to futz about yourself with Config::General, and without needing a
(Catalyst specific) context object _which you do not want to tie your
model to_.
Well volunteered, I'll use and test it when you're ready to CPAN it. :_)
Cheers
t0m
More information about the Catalyst
mailing list