[Catalyst] Reuse of controllers

David Schmidt davewood at gmx.at
Mon Sep 20 06:46:40 GMT 2010


On Mon, Sep 20, 2010 at 4:33 AM, John Romkey <romkey at apocalypse.org> wrote:
> On Sep 19, 2010, at 10:02 PM, Pavel A. Karoukin wrote:
>> I started to play more often with Catalyst framework and found that I often need to re-use some controllers logic. What the best DRY way to deal, for example, with Login/Register/Forgotpassword controllers?
>>
>> I mean I created one controller for one app. For another app I am doing I need to recreate everything, which ends up in plain copy/pasting and changing class names. I do not like this, this doesn't feel DRY. Is there some better way? Something plugable and unplugable with their own models/controller/view may be?
>
> One approach is to simply inherit from the controller you want to reuse. So, package up the controllers you want to reuse together, then use Moose to extend them in the  controller modules in each app that uses them. That allows you quite a bit of flexibility; use a common convention for the start of the dispatch chain (for instance, 'base') for each controller and then your subclass can map its parent controller to whatever path is best for each app.
>        - john romkey
>        http://www.romkey.com/
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>

Another nice approach to reuse Controller Logic is to use Moose Roles.

I got the basics from t0ms blog
(http://bobtfish.livejournal.com/#post-bobtfish-264317)
An example how i use it can be found here
http://wiki.catalystframework.org/wiki/wikicookbook/controllerwithfileupload
(the interesting part is in the file Resource.pm in the attachment
http://wiki.catalystframework.org/wiki/wikicookbook/ControllerWithFileUpload.attachment/100)


*************
package CatalystX::TraitFor::Controller::Resource;
use MooseX::MethodAttributes::Role;
use namespace::autoclean;

sub base : Chained('') PathPart('') CaptureArgs(0) {
    my ($self, $c ) = @_;
    ...
};

sub index :Chained('base') :PathPart('') :Args(0) {
    my ($self, $c ) = @_;
    ...
}
1;
*************



david



More information about the Catalyst mailing list