[Solved] Pre-Process, Before Request Controller, After Application Starts


I’m not sure I fully understand Yii’ structure. I am looking for a place to have some general pre-processing, before any request controller but after the application (and session) is loaded so I can use Yii:app().

I tried onBeginRequest, but the session is not yet loaded.

I tried in config/main.php, but again, session is not yet loaded, niether application (of course).

I tried beforeAction() in components/Controller.php, but I get a blank empty page.

Can you please help me out? Thanks.

could you paste your code using beforeAction()?

Even with

echo ‘test’;

I get a blank page. However, the command is processed, I can see ‘test’, but that’s all. I don’t know where Yii stops, no error, nothing in the logs.

please paste whole controller code… but I could guess, you do not call parent method like:

public function beforeAction() {

   echo "test";

   return parent::beforeAction();


Thanks a lot, that’s it! (Is there a good tutorial to explain all these?)

BTW, is this a good practice? What I’m doing here, is to load some generic params from the DB into the session so I can use them later in several controllers.

tutorial explaining all this is just object oriented programming in every language… when overriding a method is is goot to call parent implementation and its logic unless you know exactly that you do not want that default logic called…

you can write behavior that binds to onBeforeAction event and use that behavior in many controllers or you can override beforeAction() method in base controller class and make every controller extend that class (there is by default Controller base class in components directory)

I used beforeAction() a white until it crashed my site for some reason. Now I changed it to beforeAction($action) and return parent::beforeAction($action) at the end.

I didn’t have the time to check was was wrong and crashed but it’s working now.

you are right… it is nesessary to follow parent function signature.