S
S
serikd2017-01-15 14:19:39
Yii
serikd, 2017-01-15 14:19:39

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

1 answer(s)
I
Ilya, 2017-01-15
@rpsv

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();
    }
}

Well, and so on by analogy. In this way, your news (any) will look the same (well, given the same presentation), and you can easily produce the types of news lists you need.
You just need to understand that views (view) can (or rather should) directly take data from the model (model). If we talk about Yii, the controller in this case simply passes data to the view (which actually violates MVC slightly).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question