Attaching multiple event handlers to onBeginRequest

  1. General
  2. Researching around
  3. Solution


There came the need to perform two operations, always, on 'application boot' time. Example? My real world example involved some geo-location logic (that's the first operation) and syncing of some session details so KcFinder can be in sync with the user in context - each user with his own session container (the second).

Researching around

So anyone who ever attached a single handler to CWebApplication.onBeginRequest knows its easily done by modifying config/main.php like this (for example):

return array(
    // that's php 5.4 array notation below...
    'onBeginRequest' => ['SomeClass', 'someStaticMethod'],
    'import => [...],
    'modules' => [...],

That's nice but its not possible at the moment to attach several event handlers at the same time.


I've followed the suggestions described in this old bug report and altered index.php accordingly, so from this:


I came to this:

$webapp = Yii::createWebApplication($config);
$webapp->onBeginRequest = ['MyClassA', 'staticMethodA'];
$webapp->onBeginRequest = ['MyClassB', 'staticMethodB'];

A quick debugging revealed - both methods are being called exactly as presumed. QED.