Controller EventsΒΆ

Each of the various REST endpoint methods - create(), delete(), deleteList(), get(), getList(), patch(), update(), and replaceList() - trigger both a {methodname}.pre and a {methodname}.post event.

The “pre” event is executed after validating arguments, and will receive any arguments passed to the method; the “post” event occurs right before returning from the method, and receives the same arguments, plus the resource or collection, if applicable.

These methods are useful in the following scenarios:

  • Specifying custom HAL links
  • Aggregating additional request parameters to pass to the resource object

As an example, if you wanted to add a “describedby” HAL link to every resource or collection returned, you could do the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Methods we're interested in
$methods = array(
    'create.post',
    'get.post',
    'getList.post',
);

// Assuming $sharedEvents is a ZF2 SharedEventManager instance
$sharedEvents->attach('Paste\ApiController', $methods, function ($e) {
    $resource = $e->getParam('resource', false);
    if (!$resource) {
        $resource = $e->getParam('collection', false);
    }

    if (!$resource instanceof \PhlyRestfully\LinkCollectionAwareInterface) {
        return;
    }

    $link = new \PhlyRestfully\Link('describedby');
    $link->setRoute('paste/api/docs');
    $resource->getLinks()->add($link);
});

Project Versions

Previous topic

The ResourceEvent

Next topic

Advanced Routing

This Page