Access external class using alias


(chinaka) #1

How do i create an alias that allows me make use of an external class within my YII2 controller.

My external class is two folders before my YII2 project.

This is what i have done so far.

In my YII entry script i have this

Yii::setAlias(’@externalclass’, DIR . ‘\includes\utility.php’);

while inside a particular method in my controller i have this

echo Yii::getAlias(’@externalclass’);

this only displays the file path.

I want to be able to instantiate the external class and use its methods.


(stitch) #2

Read this first please:
https://www.yiiframework.com/doc/guide/2.0/en/concept-aliases.
Aliases are used to represent file paths or URLs so that you don’t have to hard-code absolute paths or URLs in your project.
Aliases do not include files.
But if you want, you can write:
include_once Yii::getAlias(’@externalclass’);
But it will be better to use a namespaces!