Reputation: 38332
As per this link i have almost managed to achieve the structure. But the path alias is confusing me. Could somebody explain me how can i achieve this.
http://www.yiiframework.com/wiki/155/the-directory-structure-of-the-yii-project-site/#hh5
I want my controller in frontend to access these models from common folder.
Thanks
Upvotes: 0
Views: 8653
Reputation: 880
The best way to define your own path alias is to add 'aliases' array in /config/main.php:
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'Project name',
'aliases'=>array(
'myalias'=>'/path/to/some/folder',
),
'import'=>array(
'myalias.models.*'
)
...
And then anywhere in your code you can get the path to your alias:
echo Yii::getPathOfAlias('myalias');
More info on aliases here.
Upvotes: 1
Reputation: 17478
Use setPathOfAlias()
of YiiBase class, to set a path alias:
Yii::setPathOfAlias('site', $site);
You can do this in frontend's config main.php :
$site=dirname(dirname(dirname(__FILE__))); // this will give you the / directory
Yii::setPathOfAlias('site', $site);
return array(
// the config array
// incase you want to autoload the models from common
'import'=>array(
'site.common.models.*'
)
);
As asked without auto-loading, in this case you'll have to include the model first, and only then can you instantiate it. To include we can use Yii::import($alias)
, which actually does almost the same thing(from guide):
The class definition being imported is actually not included until it is referenced for the first time (implemented via PHP autoloading mechanism).
So to use a DummyModel class which is defined in common/models/ :
Yii::import('site.common.models.DummyModel');
$model = new DummyModel;
I would suggest going with auto-loading in the main.php config itself, there is no performance drop in specifying that import array, since the model will be included only if, and when it is referenced.
Upvotes: 7