Getting data to show in different controller view


I’m using Yii2 to learn some php and build a web page with this framework. I have this issue I’m not sure what the best approach is. Since pasting code would not clarify things, I’ll describe what I want to achieve.

I have a controller Site with the index view and static pages.

I have a controller Game that shows games of a sports club with correspondig model Game.

On my Game index view i have a gridview of all games.

I would like to show on my index view (SiteController) the next game to be played. I assume I will be able to filter this on date, but currently it is unclear to me what the best way is to get this data from Game to my SiteController index view.

Where I have an action Index() with searchModel & dataProvider, I could create an action actionNextGame() and query the data i need in the GameSearch model, but i’m unsure how I can present it in the SiteController view.

Any ideas or tips or explanations how to proceed?

thanks in advance !

(PS: I based my project on Nenad’s basic template)

  1. It sounds like a widget.

  2. I’d create a separate class (usually it’s called repository) which provides data about these games to both index and other page. These could be two separate methods or a single method depending on if you want changes in one method to affect the other.

The Game model (probably created by Gii) and the corresponding CRUD controller/actions (also created by Gii) are very good starting blocks. But you don’t need to think them as a fixed set of code. The correspondence between Game (with GameSearch) model and GameController is not mandatory but arbitrary. You can use those models in any other controller including SiteController.

A very simple option is to implement some functionality in your current Game and GameSearch models that you need in your SiteController.


Thanks for your responses. I will explore the widget possiblity further but for now I created extra functions in GameSearch so it queries on the next date with a limit of 1 and show a 1row gridview on the index view.

Its a bit onorthodox the way i did but I’ll find a cleaner solution soon :)