A
A
AlexRas2017-08-10 17:53:03
Yii
AlexRas, 2017-08-10 17:53:03

What is the correct way to display the number of unread messages in yii2?

Hello.
Tell me how to do it right.
There is a small chat, messages are stored in the database.
There is a place in the header where you need to display the number of unread messages (the header is located separately in the layout).
I can only imagine such an implementation, in each controller we make a request to the database and display it in the layout.
But this will have to duplicate the code in each controller.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-08-10
@AlexRas

Option 1:
In the application config

'on beforeAction' => function(){
      //Ваш код который будет работать перед любым экшеном
    },

Option 2:
create a controller, inherit the rest from it. And it has the same beforeAction.
You can make a component and pull it, since it is needed at any application launch - this is a normal practice. In on beforeAction, we run its method, which receives and possibly caches data. Then we pass the data from it to the widget.

V
Vit, 2017-08-10
@fornit1917

Make a widget. In it, write code to calculate the number of unread messages. And in the layout, just render this widget. You don't need to duplicate anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question