Dear All,
So, I have this application structure, and I want to import all models, and components within the application1.
wwwroot
yiiapp
//START APPLICATION 1
application1
protected
config
main.php
controllers
models
view
.
.
.
themes
application1_theme
index.php
//END APPLICATION 1
//START OTHER APPLICATION
protected
config
main.php
controllers
models
.
.
.
themes
sometheme
index.php
//END OTHER APPLICATION
Now, from the OTHER APPLICATION, I want to make use models and components which I have created in APPLICATION1, how am I going to import those to my OTHER APPLICATION ? Is it even possible ?
Thank you!
pestaa
(Pestaa)
August 10, 2009, 7:15am
2
Can’t you just include the proper path information into include_path?
(Please be aware of path precedence, as Yii imports directories in such way so that they have higher priority.)
pestaa:
Can’t you just include the proper path information into include_path?
(Please be aware of path precedence, as Yii imports directories in such way so that they have higher priority.)
Hi, thanks for the reply pestaa. Yes, indeed, I could include them using the include_path. However, I was wondering if Yii itself supports importing classes across multiple applications.
Well, for now, here is how to import other classes in another application into current application:
In the config/main.php file, instead of replacing the include_path, you can just join the existing include_path with your new include path, here it goes, just insert these before the [font="Courier New"]return array([/font]
<?php
ini_set('include_path',
ini_get('include_path') . PATH_SEPARATOR .
$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'sitename'.DIRECTORY_SEPARATOR.'application1'.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'models');
The line where:
$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'sitename'.DIRECTORY_SEPARATOR.'application1'.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'models'
Outputs:
C:\www\sitename\application1\protected\models
Now, instead of importing each files one by one, you could just import a folder with all the files within it.
The line where:
<?php
ini_set('include_path',
ini_get('include_path') . PATH_SEPARATOR .
Means, you set the include_path by modifying the php.ini settings directly in the code, then get all the included path existed, and add a new include path to the existing paths.
Cheer folks!
mikl
(Mike)
August 10, 2009, 2:31pm
4
A more "Yiiish" way might be to add a custom alias and add that to the import section of your main.php:
Yii::setPathOfAlias('myOtherApp','/other/applications/path/to/protected');
return array(
// ...
'import' => array(
// ...
'myOtherApp.models.*',
// ...
);
A more "Yiiish" way might be to add a custom alias and add that to the import section of your main.php:
Yii::setPathOfAlias('myOtherApp','/other/applications/path/to/protected');
return array(
// ...
'import' => array(
// ...
'myOtherApp.models.*',
// ...
);
Finally, the more Yiish way! Yes, thank you!