Writing PHP handlers
Events
Events are fundamental to the WebSphere sMash platform but are hidden to a certain extent in the scripting environment
as each script is treated as an event handler. The PHP support in WebSphere sMash allows you to create and fire events
you define in the context of running a request using the fire_event() function.
User defined events have to be configured in the zero.config file with an event handler specified
relative to the application's root directory. The following example declares the myevent event
to be handled by the myeventhandler.php script in the public directory of the application.
/config/handlers += [{
"events" : "myevent",
"handler" : "public/event/myeventhandler.php"
}]
In PHP, the event handler is a regular PHP script that uses the global context to share contextual information about the event with the invoker. The script below sets a value in the global context for use by the handler that fired the event.
public/myeventhandler.php
<?php
// Event Handler for "myevent"
$arr =
array('foo' => 'bar');
put('/request/somekey', $arr);
?>
The following script illustrates firing the specified event with contextual information that can be accessed by the event handlers.
fireevent.php
<?php
// Fire a user defined event (myevent) from a php script.
$eventctxt =
array('otherctxt' => 'foobar');
fire_event('myevent', $eventctxt);
// Get a value stored by the event handler for verification
$arr = zget('/request/somekey');
zput('/request/headers/out/Content-Type', 'text/plain');
foreach($arr as $key => $value) {
echo " $key $value";
}
?>