Defining Actions

Table of Contents

Method: preDispatch and postDispatch

As mentioned in the previous chapter, you typically won't need to define the run() method yourself. The default implementation will use the next value in the requested path.

It will look for a method in the driver called someAction().

For example, if the request URL is /__/weather/get/Adelaide, the first URL part after the driver name is get. This would result in a function called getAction() being called.

The remaining path is passed to this method (that is Adelaide).

If the request URL is simply /__/weather, the default action that is request is indexAction().

If a given method isn't found then an exception is thrown from run(), resulting in a 404 file not found response.

To continue with the weather example, the following listing demonstrates how to define an action. Let's assume that the request URL for this would /__/weather/get and that it expects a posted value called city

Example 4.1. Defining a backend request action (driver.php)

    class Driver_backend_requests_weather_controller extends Module_Backend_Driver_Abstract
        public function getDependentModules()
            return array('weather');
        public function indexAction($path)
            $request = $this->getPost();
            $city    = $request->getPost('city');
            // now make use of $city

You can have as many actions as you like, each of which can accept different values and return any type of data. We'll look at how to properly respond to a request shortly.

Method: preDispatch and postDispatch

  • public function preDispatch($action)

  • public function postDispatch($action)

The preDispatch() method is called immediately before an action handler is called, while postDispatch() is called immediately after an action handler is called.

The name of the action is passed as the only argument to these methods.

For example, if the requested URL is /__/weather/get, then the following calls occur:

  1. $this->preDispatch('get');

  2. $this->getAction($path);

  3. $this->postDispatch('get');

These methods allow you to easily define functionality that is common to all action handlers.