[Catalyst] Why is $c undefined?

Ian Docherty catalyst at iandocherty.com
Tue Oct 30 07:22:11 GMT 2012


On 29 October 2012 21:01, Craig Chant <craig at homeloanpartnership.com> wrote:
...
>
> I have read and seen frameworks such as Mojolicious encourage a shrinkage of the Model and move alot of functionality to the Controller, so there is a paradigm which seems to imply it is ok to do more stuff in the Controller, but I am leaning towards having the main code in the Model and then bolting it together via the Controller.
>
I can understand why you get this impression, I think a lot of people
end up putting code in the Controller when they first start using MVC
(I did so myself in the past).

The Model should be external to your Catalyst app (or whatever
framework you use) so that you can use it in things like cron jobs. It
also makes testing easier if your Model is separate from your Catalyst
app. Look at using something like Catalyst::Model::Adaptor as a thin
shell to add your external Model into Catalyst.

I am moving more and more into making my Controllers as thin as
possible. Logic that I might have previously put into the Controller,
I either put into the Model or I create helper functions. Here is an
example of a Controller (from Mojolicious as it happens but that is
not important)

sub user_list {
    my ($self) = @_;

    $self->jqgrid->render($self, {
        rs      => $self->schema->resultset('User')->search_rs,
        filters => {},
        rows    => [qw(id name)],
    });
}

It's not important to know what is going on here, but this Controller
gets a list of all users, formats the data for use in the jQuery
jqGrid allows for sorting and filtering and outputs the data in JSON
format. The point being, the controller code is kept simple and 'thin'
and yet it does a lot of work behind the scenes.



More information about the Catalyst mailing list