问题
I'm trying to access the Catalyst config hash from within a model like this (contrived example):
package Dabadie::Model::DirFind;
use Moose;
use namespace::autoclean;
extends 'Catalyst::Model';use namespace::autoclean;
sub list {
my ($self, $c) = @_;
return $c->config();
}
however, $c is undef, and $self->config returns nothing.
Can anyone help figure this one out?
Thanks,
Simone
回答1:
This is as intended - your model is supposed to be separable from your controller logic as per the MVC design philosophy.
You could pass $c as a parameter when you initialise the model in your controller code but this will lead you down the 'interconnected' model and controller design that violates the pattern, but it may make sense to do this - is your model setting config parameters or just receiving them? If you are setting config parameters via the model you may have a design issue.
Or just pass the config hash as an argument instead on model initialisation:
$c->model('AppModel')->new(config => $configParameters);
This would require you to declare a moose attribute in your model to capture the config parameter on construction.
has 'config' ( isa => 'HASH', is => 'ro' );
More on Moose attributes here
来源:https://stackoverflow.com/questions/11776087/access-config-from-inside-catalyst-model