ololo
(Laa513)
July 30, 2011, 8:31pm
1
It often happens when you have to fix some imports in code of an extension to make it work in your application. People don’t use relative imports because there is no straightforward way to do this (setting an alias for extension’s folder like Yii::setPathOfAlias(‘self’, DIR ) is not quite straightforward, and this may override other aliases).
I think that it worth to add YiiBase::getAliasOfPath() method, which returns aliases for pathes within the application’s folder. A reference to the getAliasOfPath() in documentations with mention of using relative imports can help make code of extensions better and life easier
Example:
$self = YiiBase::getAliasOfPath(__DIR__);
Yii::import("$self.helpers.MyHelper");
Yii::import("$self.components.behaviors.XxxAble");
Thank you
samdark
(Alexander Makarov)
July 31, 2011, 11:42am
2
Interesting feature. Can you add it here? http://code.google.com/p/yii/issues/list
Ben
(Benjamin Woester)
July 31, 2011, 1:42pm
3
Generating an alias for the sole purpose of having it resolved by import()?
Instead I would suggest to modify the import-method, so that it also takes pathnames as parameter:
// importing class
Yii::import(__DIR__.'/helpers/MyHelper.php');
// importing folder
Yii::import(__DIR__.'/components/behaviors');
alex-w
(Pr0j3ct A1ex)
August 2, 2011, 7:55am
4
I think slashes could cause problems with the namespace code in import.
What about an extra parameter to specify a base directory?
Yii::import("helpers.MyHelper", false, __DIR__);
Yii::import("components.behaviors.XxxAble", false, __DIR__);
ololo
(Laa513)
August 2, 2011, 9:52am
5
php namespaces looks the same as windows pathes, aliases also are more readable and there is no differences for linux/windows…
alex-w, as a variant
$self = YiiBase::getAliasOfPath(__DIR__);
Yii::import("$self.components.helpers.ArrayHelper");
// VS
Yii::import('components.helpers.ArrayHelper', false, __DIR__);
and
$self = YiiBase::getAliasOfPath(__DIR__);
Yii::import("$self.components.helpers.ArrayHelper");
Yii::import("$self.components.helpers.StringHelper");
Yii::import("$self.components.behaviors.XxxAble");
Yii::import("$self.components.behaviors.YyyyyyyyyyAble");
Yii::import("$self.components.behaviors.ZzzzzAble");
Yii::import("$self.components.Debug");
Yii::import("$self.something.Usefule");
Yii::import("$self.and.one.more.Import");
// VS
Yii::import('components.helpers.ArrayHelper', false, __DIR__);
Yii::import('components.helpers.StringHelper', false, __DIR__);
Yii::import('components.behaviors.XxxAble', false, __DIR__);
Yii::import('components.behaviors.YyyyyyyyyyAble', false, __DIR__);
Yii::import('components.behaviors.ZzzzzAble', false, __DIR__);
Yii::import('components.Debug', false, __DIR__);
Yii::import('something.Usefule', false, __DIR__);
Yii::import('and.one.more.Import', false, __DIR__);
alex-w
(Pr0j3ct A1ex)
August 2, 2011, 2:10pm
6
Could add a new function called "imports", would work from an array of aliases with a base directory.
Yii::imports(array(
'components.helpers.ArrayHelper',
'components.helpers.StringHelper',
'components.behaviors.XxxAble',
'components.behaviors.YyyyyyyyyyAble',
'components.behaviors.ZzzzzAble',
'components.Debug',
'something.Usefule',
'and.one.more.Import'),
false, __DIR__);
ololo
(Laa513)
August 4, 2011, 9:56pm
7
or could make YiiBase::import() working both with strings and arrays, and introduce third parameter $baseDir
function import(mixed $aliases, boolean $forceInclude = false, string $baseDir = null)
So this is compatible with current import method, adds a new feature for imports within extensions and makes code shorter.
it seems good