Use my own Html helper instead of yii\helpers\Html


I have my own version of Html helper which extends yii\helpers\Html.

Is there any way to make Yii use my class instead of yii\helpers\Html without modifying the source of Yii directly? I mean in default widgets like ActiveForm and ActiveField.

I dont think so. You must either replace the file or use new namespace and class-name.

OK, I just found a way to do that! :rolleyes:

Added this code to the config file:

Yii::$classMap['yii\helpers\Html'] = '@my/Html.php';

and had my Html class extending the original BaseHtml.

This way instead of loading the Html class from Yii, it loads my own class.

Thanks anyway :)

Thats one way of doing it.

Another would be to extend classes like ActiveForm/ActiveField from the framework into your own system/application.

yes that was my first try. But it turned out I had to copy and paste all the code into my class. it would make my code less compatible with possible updates to the framework.

my second solution was to include my Html helper as yii/helper/Html in the bootstrap file. it worked, but wasn’t very elegant :D