FAQ
Hello,

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?

Regards,
Pavel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20100919/46a41466/attachment.htm

Search Discussions

  • John Romkey at Sep 20, 2010 at 2:33 am

    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/
  • David Schmidt at Sep 20, 2010 at 6:46 am

    On Mon, Sep 20, 2010 at 4:33 AM, John Romkey 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@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
  • Pavel A. Karoukin at Sep 20, 2010 at 1:28 pm

    On Mon, Sep 20, 2010 at 1:46 AM, David Schmidt wrote:
    On Mon, Sep 20, 2010 at 4:33 AM, John Romkey 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@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;
    *************


    Thank you David. This looks very close to what I had in head. I will look
    more in to it!

    Regards,
    Pavel
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20100920/b45efad3/attachment.htm
  • Eden Cardim at Sep 21, 2010 at 1:59 am
    "Pavel" == Pavel A Karoukin writes:
    Pavel> I got the basics from t0ms blog
    Pavel> (http://bobtfish.livejournal.com/#post-bobtfish-264317) An
    Pavel> example how i use it can be found here
    Pavel> http://wiki.catalystframework.org/wiki/wikicookbook/controllerwithfileupload
    Pavel> (the interesting part is in the file Resource.pm in the
    Pavel> attachment
    Pavel> http://wiki.catalystframework.org/wiki/wikicookbook/ControllerWithFileUpload.attachment/100)

    An overlooked feature of Catalyst is the ability to modify actions via
    the configuration:

    package MyRole::Login;
    use Moose::Role;

    sub login { etc... }

    package MyApp::Controller::Login;
    use Moose;
    with 'MyRole::Login';

    __PACKAGE__->config(action => { login => { PathPart => 'sign-in' } } );

    package MyOtherApp::Controller::Login;
    use Moose;
    with 'MyRole::Login';

    __PACKAGE__->config(action => { login => { PathPart => 'log-in } } );

    note that you can use any key that you would use as an action attribute,
    including chainining, and catalyst automatically promotes the method to
    an action due to the existence of the config.

    --
    Eden Cardim Need help with your Catalyst or DBIx::Class project?
    Code Monkey http://www.shadowcat.co.uk/catalyst/
    Shadowcat Systems Ltd. Want a managed development or deployment platform?
    http://blog.edencardim.com/ http://www.shadowcat.co.uk/servers/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedSep 20, '10 at 2:02a
activeSep 21, '10 at 1:59a
posts5
users5
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase