yii\helpers\Html autoloading in views

Hello Everybody

Is it possible to autolad the Html-Helper (or any other helpers) automaticly to my views in yii?

For me it looks a little bit overloaded to include the namespaces of the usefull helpers (e.g. Html) i need always in each view.

Thank you for your help.


Load them in the layout?

All Yii (and vendor) classes are autoloded automatically, the “use” statements have nothing to do with autoloading. Unfortunately that’s the way PHP namespaces work. The only way to get rid of them is to declare your own classes in the global namespace extending framewok’s helper classes. I’m not sure if it’s worth the effort.


Sorry, my fault. I was not clear enough in my question.

Now, when i need to use e.g. the Html-Helper I need to load it over "use yii\helpers\Html;" in each view separatly.

This is a repeat in all my views. is there not an other solution for that? e.g. to say to the view component: everytime you are loaded, please load automaticly (without useing "use" in all the views) some classes.

Thank you for your help.

It won’t solve your problem because “use” statements don’t actually “load” anything. They don’t even trigger the autoloading mehcanism. Their only purpose is to tell the PHP parser that which one of the possible classes is referred as “Html” in your code (is it \yii\helpers\Html, \some\other\vendor\Html or \my\custom\Html ?). And you have to declare it in every file because the alias “Html” can stand for different fully qualified classnames in different files and there’s no such thing as application-wide global “use” statement in PHP.

I get your point. But still, this doesnt change that it looks "ugly" for me to use such namespace includings in a view directly. if i started to use the \yii\helpers\Html in all my views, then i decid to inherit from \yii\helpers\Html my own Html-Helper Class, this means, i need to change in all my views the path…

It looks like, Im alone with this worries.

This is just part of the mental-floss that comes with using newer versions of php.

Look around, symfony, laravel both ‘use’ the same constructs.

You’ll get used to it.

But you have to change it only once per file. Prior to the introduction of namespaces in PHP you had to change every occurence of the classname.

My IDE generates use statements automatically so I don’t care too much. And I’m already used to the ugliness of PHP :)

Which IDE are you using?


Hi, yes your right. I dont have a problem to "use" for namespacing usw. Thats fine for me if its in the Controllers, Models or other "locigal" or "business relevant" Codes.

My only problem is to use it in Views. Because symfony uses twig they dont have the same issue like in yii. It’s a concept decision and because yii uses as is default View-Renderer plain PHP. I know, Twige is an official extension, but feels for me more complicated to use with widgets and all the great helpers of Yii.