Complex session include problem

Project desc:

A website that lets you filter price results on a page (non ajax for now). The filters are stored in sessions and in a cookie.

What i have:

/index.php includes a globals.php file before the yii application is loaded. It holds functions to add/remove/read session vars via yii’s Yii::app()->session

/protected/views/layouts/main.php includes a sessions.php file before the rest of the website is loaded

When a user changes the filters (input checkboxes) the page generates a post request (to the same page) with all the selected filters. The post gets catched in the sessions.php file where it handles the selected posts, stores in the session and then writes it to a cookie.

Then the (same/previous) page gets rendered with the filters. The filters get generated with a function in the globals.php file called filters. The filters function reads the session and generates the filters.

The current problem:

You can guess what is going wrong. Because the globals.php file is included before the session.php the function filter (in globals.php) reads old sessionData.

This can be fixed by moving the filters function after the post processing in session.php but then the views (whichs are generated before the layouts/main.php) complain that they can not find the filter function.

I tried fixing this by moving the session.php file to the /index.php. Placing the session.php before the application is rendered (Yii::createWebApplication) generates an error explaining that the Yii::app() (and thus the session) is not avalible. Placing the session.php after the application isn’t an option either.

I have also tried to place the post handling in the globals.php file but setting the session does not work when the application isn’t loaded yet.

What I want:

A solution for the problem of course ;) I want to know if there is a place in the code (my own code, not the included framework) where I can include files. This needs to be between the rendering of the application and before rendering the controllers and views.

I have tried to include the session.php file the main config file but this did not have an effect (the functions were not available in the views or controllers so my guess is the yii loads those components later too).

Any hits/tips are welcome!



Found a solution by adding a onbeginRequest in the config file which loads the sessionController i have created. The sessionController reads the $_POST on init and updates the current session and cookies. Works perfectly because these actions get processed before controllers and views are loaded!

'behaviors' => array(

	'onbeginRequest' => array(

		'class' => 'application.components.SessionController',