Answer the question
In order to leave comments, you need to log in
Where and how best to collect a model show?
The template is advanced, but it probably won't matter from version 3.
There are ordinary AR models in common, in the frontend and backend they are practically not replaced,
almost naked, i.e. As we store in a DB so are given.
Now you need to show it all in view, give it json or something else. For the API, it would probably be appropriate to give as is +/- small helpers, but for HTML (frontend / backend) what is the best way to do it?
1. Store views directly in the model
<?php
echo $post->publicDate(); // <date format="***">23 янв 2019</date>
echo $post->icon(); // <span class="icon icon-hot"></date>
echo $post->shortCardView(); // <artcile>.$title..$descr..$date</artclie>
\yii\helpers\Html
and use something likePostHtml
<?php
echo PostHtml::publicDate($post);
echo PostHtml::icon($post);
echo PostHtml::shortCardView($post);
$postHtml = PostHtml::create($post);
echo $postHtml->publicDate();
echo $postHtml->icon();
echo $postHtml->shortCardView();
<?php
echo PostWidget::publicDate($post);
echo PostWidget::icon($post);
echo PostWidget::shortCardView($post);
Answer the question
In order to leave comments, you need to log in
Definitely not, if for some reason you don’t want to see it in the view, then we pack it either in the Helper or in the Widget.
The choice between a widget and a helper depends on the complexity of the layout and / or the presence of dependencies (js files, fonts, etc.).
In the context of this question and now it does not matter if it is advanced or basic or some other template.
I can’t imagine a situation in which we transfer data from the controller in Json format. This is only for ajax requests, but then there is no view as such.
Well, if this is a REST api, then there is a different controller and models have additional methods. I don't quite understand what "as is" means.
Greetings. In models, the layout to store is in any case bad. Violation of MVC in its purest form :)
I transfer such actions to widgets.
Between a helper and a widget, I would still choose a widget. This is if some kind of logic should be in the code. Type, selection of the active element.
Otherwise, you can put it in a separate view, somewhere in common and render in other views
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question