Access config from inside Catalyst Model

若如初见. 提交于 2019-12-11 06:19:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!