Facads in Yii2

Hello everybody

I have this questions:

  1. Is it possible to make "facades" like in laravel in yii?

  2. Can I autoload the yii Html helpers automaticly with my view-Components without to load it over the "yii\helpers\Html" namespace in every view i need it? Problem is following:

I have 3 partials for my layout:

  • header.php

  • content.php

  • footer.php

In each file i have to load the most useful Html helper manuelly. Is there an other way (an other autoloading way which loads this helper automaticly ?)

Thanks

Cihan

I don’t use Laravel but as far as I understand its “facades” are simple static wrappers around application components. You can write similar static classes relatively easily, but I can’t really see why would you need them in a Yii application.

No, you don’t have to. Yii’s autoloader loads them automatically. The “use” statements have nothing to do with class loading and you can omit them if you want, but if you do, you have to use the fully qualified name of the class every time, which can be inconvenient.

Thank you for your response. I get your point.

But would it not be nicer to access this kind of helpers over the view object directly in a non static way?

e.g.

$this->html->img(‘xyz’);

Best

Cihan

Static Helpers versus Application components on GitHub.

I think that current implementation is shorter:


Html::img(..)

and you may select own Html helper.