Yii 1.1: Attaching multiple event handlers to onBeginRequest

2 followers

General

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.

Solution

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

require_once($yii);
Yii::createWebApplication($config)->run();

I came to this:

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

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

Be the first person to leave a comment

Please to leave your comment.

Write new article
  • Written by: Boaz
  • Updated by: CeBe
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +2
  • Viewed: 2,067 times
  • Created on: Mar 13, 2014
  • Last updated: Apr 14, 2014
  • Tags: events, event handler