Mach-II for PHP: A Preview - Event Handlers
(Page 5 of 7 )
Let’s examine <event-handlers> in mach-ii.xml. As you already know, event-handlers are where you define the events triggered during a request. They describe the event names, and which views to associate with them.
<!-- EVENT-HANDLERS -->
<event-handlers>
<!-- Main (landing page) -->
<event-handler event="showMain" access="public">
<event-arg name="page" value="Main" />
<view-page name="main" />
</event-handler>
<!-- Process a few form variables for demonstration purposes -->
<event-handler event="processForm" access="public">
<event-mapping event="success" mapping="showResults" />
<event-mapping event="noSuccess" mapping="showMain" />
<notify listener="processForm" method="validateForm" resultKey="GLOBALS['results']" />
</event-handler>
<!-- Display results of form processing -->
<event-handler event="showResults" access="public">
<event-arg name="page" value="Success" />
<view-page name="results" />
</event-handler>
<event-handler event="exception">
<event-arg name="page" value="Failure" />
<view-page name="exception" />
</event-handler>
</event-handlers>
Figure 5 mach-ii.xml <event-handlers>
Obviously our application needs to have a landing page. Since we named our default event showMain, we’ve defined a showMain event handler to receive that default event:
<!-- Landing Page -->
<event-handler event="showMain" access="public">
<event-arg name="page" value="Main" />
<view-page name="main" />
</event-handler>
Figure 6 Our landing page’s event-handler
Events can also be announced in <event-handler>, allowing very complex interactions between objects.
Inside <event-handler> is a node named <view-page>. This calls views defined in <page-views>. This is where display files are organized.
<!-- PAGE-VIEWS -->
<page-views>
<page-view name="main" page="/views/main.php" />
<page-view name="showResults" page="/views/showResults.php" />
<page-view name="exceptionEvent" page="/views/exception.php" />
</page-views>
Figure 7 page-views node
You’ll find yourself coding very specific event-handlers for each specific type of request. Similar to coding a method in a PHP class, event-handlers should be as concise and reusable as possible. It’s important to factor in the needs of your application when determining the granularity of your event-handlers.
The event handler we just defined will communicate with a <listener> we’ve created (below).
<!-- LISTENERS -->
<listeners>
<listener name="processForm" type="phpMachII.model.processForm">
<invoker type="MachII.framework.invokers.ObjectEvent" />
</listener>
</listeners>
Figure 8 Listeners node
Next: Basic Coding Techniques >>
More PHP Articles
More By Mike Britton