L
L
Lander2017-09-12 10:55:44
Yii
Lander, 2017-09-12 10:55:44

How to add custom properties to View from Yii2?

Good afternoon.
For the entire application, I want to add another View-> subTitle to the View along with View-> title.
The first solution that came to mind is to inherit from yii\web\View, add a new property to the heir and redefine the View base class to your own through classMap. But I read that such a solution is a "dirty hack".
In this regard, the question is: How to extend the base View class with its properties in a kosher way? Or, perhaps, I needlessly complicate everything and there is some kind of ready-made method?
Thanks in advance to all who answer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Karavaev, 2017-09-12
@usdglander

You can implement parameter passing via View::params. How it is implemented for the same breadcrumbs. And do not torture yourself with attempts to reconfigure and inherit from the base class for the sake of adding fields. But there is no "dirt" in inheritance either. This is OOP and inheritance is one of the basic principles here, how can it be a dirty trick? The only question is that in the future you would use your View everywhere and not get errors if you suddenly get the base one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question