ActiveRecords on main.php

I have a table “school_year” that has id & description (… ,‘2013-2014’,‘2014-2015’) fields. I want a dropdown list on every page (figured i’d set it on main.php). I have a model SchoolYear.php.

On my SiteController I grabbed all the school years after successful login. (is this correct place for this?)

How/where should I create the dropdownlist? Is main.php the wrong place?

Eventually I want to set the "selected_school_year" in session var and change when someone switches school_year.

Should I use active form on main.php?

I am new to yii (using v2) so any help is greatly appreciated.

Yes that is a perfectly acceptable place for that. For example I just completed a project where the site has a franchise selector that shows up on every page and so I put it in the main.php layout file and works perfectly.

Thanks for the reply. So on main, should I "use app\models\SchoolYear" and generate the list or do it on the SiteController/login and save to session? (in your opinion)…

Either solution would work.

Thanks for your input. I have this working fine now.

Now I need to set a session variable for “selectedSchoolYear” so if a user changes school years, they can view data from that year instead. I would normally use jquery/ajax to do this, but don’t know the Yii2 way. There is not much documentation on jquery/ajax integration yet, any pointers?

Edit: I also wish to reload the current page so it will use the updated session information. I

Its perfectly acceptable to use the Ajax/Javascript method. Unless you want to create a form and do a submit with the new school year. But that would force a full screen refresh. Unless you use Pjax. But that’s a whole different thing altogether.

So I’d definitely go the Javascritp/ajax route. You can do a quick update of some data in the browser and on the server set the session information so that for subsequent page loads the session data will have the correct information.

Do you know of any good tutorials on js/ajax integration in Yii2? I cannot find any as of yet.