Majky
Majky

Reputation: 2013

Yii Share models between two applications

I've two web applications which are located on one server but on different domains (for example app A is administration and app B is client).

The problem is that I want to share models (ActiveRecords) from application A to be available in application B.

Is there any clever way to do this?

Thanks!

Upvotes: 1

Views: 723

Answers (3)

Jon
Jon

Reputation: 437376

Sure, just follow a few easy steps:

1. Put your models in a shared directory

For example, if your current directory structure looks like this:

/www
    /application1
        /protected
            /models
    /application2
        /protected
            /models

Create another "shared" directory. It's a good idea to put some structure in there as well, in case you want to share more than just some models:

/www
    /application1
        /protected
            /models
    /application2
        /protected
            /models
    /shared
        /models

Put the active record models you want to share in /www/shared/models.

2. Alias the shared directory in both applications

Go to your main.php configuration file in both applications and create an alias for the shared directory:

Yii::setPathOfAlias('shared','../shared/'); // or use an absolute path

3. Import shared models

Still in your main.php configuration, import your shared models:

'import'=>array(
    // ...existing imports here...
    'shared.models.*',
),

You can now directly refer to the shared classes anywhere in your application and Yii will load the appropriate classes automatically.

If you later add more directories to /shared then simply add corresponding lines to the import configuration.

Upvotes: 3

ineersa
ineersa

Reputation: 3435

Yii::setPathOfAlias('applicationA','path/to/applicationA/protected');

Then when you making import in config:

'import' => array('applicationA.models.*'....

Now you will be able to use models from appA in appB.

Same can be done with modules, controllers and views. Views - viewPath Modules - modulePath Controllers - in index.php add

$app->setControllerPath('////protected/controllers');

Before $app->run();

Upvotes: 1

Jakub Matczak
Jakub Matczak

Reputation: 15656

Try to make an alias in one app to the second ( And from the 2nd to the 1st ;) ) using YiiBase::setPathOfAlias()

Documentation: http://www.yiiframework.com/doc/api/1.1/YiiBase#setPathOfAlias-detail

Upvotes: 3

Related Questions