srchulo
srchulo

Reputation: 5203

Catalyst reuse app code with multiple sites

I have several sites that all have very similar functionality. They allow a user to register, login, enter credit card information, and other common functionalities. The only parts that will be different are the design (templates) and some backend administrative functionality (essentially the area once logged in will perform different tasks), and each site will need its own database connect information. Is there a way to reuse this code effectively so I don't have to copy and paste? Should I be pointing these websites to the same Catalyst app, and then somehow handle only the different aspects differently based on the domain name that called on the Catalyst app. Or is it better to just have separate Catalyst apps and copy the code, since once logged in the sites will be different. I'm wondering if anyone has ever done anything similar to this before and knows the best way to handle it. Thanks!

Upvotes: 1

Views: 113

Answers (1)

RET
RET

Reputation: 9188

I've got some experience with this. We have a series of applications that share some common base code for the CRUD functionality.

You just configure your perl paths and TT paths in such a fashion that modules and templates in the app override any equivalent in the common repo, and app modules can inherit from the common modules if/as required, eg:

/var/common
           /root
           /lib
           /and-so-on
/var/myapp1
           /root
           /lib
           /and-so-on
/var/myapp2
           /root
           /lib
           /and-so-on

Your main program MyApp2.pm has something like:

use lib '/var/common/lib';
use lib '/var/myapp2/lib'; # use lib behaves like unshift

Your View::TT config has something like:

include_path => [ '/var/myapp2/root', '/var/common/root' ];

And your modules can do things like:

package MyApp2::Controller::Foo;

use base 'Common::Controller::Foo';

...if you want to use most of the Common Foo but override certain aspects.

With the include_path setting, you can share common template stuff, and override it in the same way:

[%- PROCESS bar.tt -%]

will output /var/myapp2/root/bar.tt if it exists, or fall back to /var/common/root/bar.tt if it doesn't.

We've found this to be very elegant, effective and most of all, DRY.


UPDATE

An example to answer a question in the comment-trail about inheriting/overriding:

package MyApp2::Controller::Foo;

use strict;
use warnings;

use base 'Common::Controller::Foo';

sub baz :Local {
    my ($self, $c) = (shift, shift);
    # set something up that's peculiar to MyApp2's implementation of baz
    $c->stash->{bazconfig} = { ... };
    # ...
    $self->SUPER::baz($c,@_);
    # do something else peculiar
    # ...
}

sub quux :Local {
    my ($self, $c) = (shift, shift);
    # completely override the Common quux action
    # etc
    # ...
}

1;

Upvotes: 2

Related Questions