Answer the question
In order to leave comments, you need to log in
What is the best way to implement actions in framework controllers or widgets?
Hello.
What is the best way to implement this - for example, we have a page on which we have top news (top), a list of current news (middle) and archive news at the bottom, a comments feed on the right?
In a specific case, I work with the Yii2 framework, I implemented the top and comments as widgets, and the listing of news in the controller action.
Now we need to implement the bottom - archived news, and I think it would be better to do it as a widget or add it to the controller action.
How is this implemented, for example, in other frameworks where there are no widgets like in Yii2?
Answer the question
In order to leave comments, you need to log in
Widgets - simply display the data that the controller threw to them. At their core, widgets are views (MVC). That is, in the action (action) controller, you only prepare the data (model) for the view.
Speaking specifically about your task and a bit of OOP: you need to implement 2 basic widgets:
- a list of news - takes a DataProvider with news as input
- a list of comments - takes a DataProvider with comments as input
And then inherit from the "news list" widget and do a couple more :
- list of top news
- list of archived news
The code will look something like this (abstract):
/*
список новостей
*/
class ListNews extends ListView {}
/*
список топовых новостей
*/
class ListTopNews extends ListNews
{
public function init()
{
// создаете провайдер с топовыми новостями
$this->dataProvider = new ActiveDataProvider(...);
parent::init();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question