Sharing catalyst components between applications in the same filesystem hierarchy

StackOverflow https://stackoverflow.com/questions/22255941

  •  11-06-2023
  •  | 
  •  

Frage

I'm trying to share components across multiple catalyst applications that are in the same directory level.

For example, if I have an application AppOne, the class hierarchy for models might look like so:

AppOne::Model::DB

and the filesystem hierarchy would look something like this:

./lib/AppOne/Model/DB.pm

And to use it in a AppOne controller (AppOne::Controller::Foo.pm, for example):

$c->model('DB::SomeTable')->all()

If I had a second application AppTwo, that was built in the same filesystem heirarchy as AppOne, is it possible to use the AppOne::Model::DB.pm in an AppTwo controller?

In other words, how can I tell the action in ./lib/AppTwo/Controller/Root.pm that I want to use the models from .lib/AppOne/Models?

War es hilfreich?

Lösung

For this to work you need two things.

  1. The script that starts your Catalyst application needs to be able to find the model in the Perl library path (i.e. @INC)
  2. You need to configure Catalyst to re-use a different model outside of where it will normally find it.

Below I detail one possible approach to this. There are many different ways do this depending on what fits your needs.

Create a "super-project" or folder that will contain the common model, AppOne and AppTwo. AppOne and AppTwo are two normal Catalyst projects with the default folder structure. To store the common model create the folder common/lib in the "super-project".

Modify the AppOne/script/appone_server.pl (or what other script you use to starter your Catalyst server so that it can find the common/lib folder. You can do this be adding the lines

use FindBin;
use lib "$FindBin::Bin/../../common/lib"; # now AppOne can find the lib.

Add a Catalyst model to AppOne/lib/Model called CommonModel like this.

package AppOne::Model::CommonModel;

use strict;
use base 'Catalyst::Model::DBIC::Schema';
__PACKAGE__->config(
    schema_class => 'CommonSchema', # name of the schema class in common/lib    
    connect_info => {
        # connect info here
    }
);

Repeat the two last steps for AppTwo as well.

As mentioned this is one way to do it. It assumes that you will deploy you applications by just copying the entire super-project to your server and that the directory structure will be exactly the same.

Andere Tipps

I guess this was already answered, but I'll put in my answer as well.

in MyApp/lib/MyApp/Model/ADMIN.pm

package MyApp::Model::ADMIN;
use strict;

use lib "/sharedloc/perlmodules/AdminModel/lib";

use AdminModel; 
use base 'Catalyst::Model::DBIC::Schema';

__PACKAGE__->config(
    schema_class => 'AdminModel',

    connect_info => {
        dsn => 'dbi:mysql:administrationdb:mysql.example.com',
        user => 'user',
        password => 'password',
        AutoCommit => q{1},
    }    );

And in your controller or whereever

$c->model('ADMIN::User')->search;

Your model should be created using the DBIx::Class schema loader, not h2xs or some other perl module creator.

mkdir AdminModel
cd AdminModel
perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib \
    -e 'make_schema_at("AdminModel", { debug => 1 }, \
    [ "dbi:mysql:administrationdb:mysql.example","user", "password" ])'

If you have lots of custom modules you can add them all in MyApp/lib/MyApp.pm, or in whatever startup script you are using (server.pl or fastcgi.pl) or install them with cpan or cpanm.

Personally I prefer to have all my shared utilities in custom perl modules completely outside of the catalyst skeleton. Then the application gets whichever libs it needs at runtime. For models use DBIx::Class::Schema, and for everything else build your modules using h2xs or module-starter (or whatever you want). Tar them, and you can install them using cpan or cpanm. Install Modules Manually CPAN or Install Modules Manually cpanm I use cpanm because it can install straight from git.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top