simone
simone

Reputation: 5227

Why can't I access a catalyst model's config from inside the model itself?

I have a class that looks like this:

package MyApp::Model::Skunk::Find::Images;

use Moose;
use namespace::autoclean;

extends 'Catalyst::Model::Factory';

use Data::Dump qw/dump/;

sub prepare_arguments {
    my ($self, $c) = @_;

    # these are for debugging purposes only

    $c->log->info("package: " . __PACKAGE__);
    $c->log->info("catalyst config: " . dump $c->config);
    $c->log->info("images class: " . __PACKAGE__->config->{class});
    $c->log->info("images root: " .  __PACKAGE__->config->{root});

    return { root => __PACKAGE__->config->{root} };
}

and myapp.conf looks like this (snippet):

<Model::Skunk::Find::Images>
    root    '/foo/bar/myapp/data/img'
    class   MyApp::Find
</Model::Skunk::Find::Images>

I can access the whole application config hash (I am aware of this answer), but I cannot seem to be able to access the class' config directly, so

 $c->log->info("images class: " . __PACKAGE__->config->{class});
 $c->log->info("images root: " .  __PACKAGE__->config->{root});

from within MyApp::Skunk::Find::Images both produce nothing.

If I configure the variables from inside the package like this:

__PACKAGE__->config( 
                    class => 'MyApp::Find',
                    root  => '/foo/bar/myapp/data/img'
                   );

everything works fine.

Can anyone help explain?

Upvotes: 1

Views: 530

Answers (1)

hobbs
hobbs

Reputation: 240531

The config for a Catalyst component is passed to its constructor. You should just do in your Model:

has ['class','root'] => (
   isa => 'Str',
   is => 'ro',
);

and in your methods use $self->class and $self->root. Accessing __PACKAGE__->config within a component (except once, during startup, to set config) is a mistake. Looking in $c->config is even further off the mark.

Upvotes: 1

Related Questions