I have a very large code I twist, to make it more readable, I part of the code I put into another file and then insert it into the twist.

In the main view, I connect the model use app\models\options; then below the code I connect the file (template) in this way:

 <?php require '../library/site_templ/event/add_event_editor.php'; ?> 

An error occurs in this pattern:

Class 'options' not found

It turns out that after carrying out a part of the code, should I also take out and announcements of all models, etc.? After all, I announced the model in the parent view, why connect it again?

    2 answers 2

    As a rule, the model is accessed from the controller, assigned to a variable, and transferred to the view file. If the presentation file is large, most likely some logic has arisen there, which should not be there. Usually, presentation files receive data and display it, and all the data retrieval conditions in model methods.

    If the view file is still large, it probably makes sense to create a widget in which to put some of the code to display the data.

    Anyone who continues to use require in presentation files will read the framework documentation again. There is a render, renderPartial, renderFile .

    Details about the types and patterns.

    • too large layout at the page for adding an event, a lot of inputs, lists, etc. The code is adaptive naturally and it still creates confusion. The widget is not a solution, how many do I need to write them 5-10, and why should it be if it is mainly layout. Here also I consult as it is better for making, while I do c require. How do template engines work in Yii? - Sergey P
    • @SergeyP Added a link in the answer - fedornabilkin
    • Thank you for what you need. - Sergey P

    In each view you need to prescribe your own use app\models\options ; You can also pass a model from another view to the view.

    view1.php

     $this->render('view2', ['model' => $model]); 

    view2.php

     echo $model->property;