...
Triggering an event invokes all callbacks that have been registered for that event. To trigger the "EventName" event invoke the Events::trigger() method and pass in any parameters that you want each an instance of the callbacks to receive EventObject that will be passed to each callback.
Code Block |
---|
language | php |
---|
title | Somewhere in your code |
---|
|
$this->Events->trigger$event = new EventObject("EventName", array("square", "pink"));
$this->Events->trigger($event); |
Here's a complete example that will output "square pink".
Code Block |
---|
language | php |
---|
title | /app/controllers/my_controller.php |
---|
firstline | 1 |
---|
linenumbers | true |
---|
|
<?php
class MyController extends AppController {
public function index() {
$this->Events->register("EventName", array($this, "callbackMethod"));
$event $this->Events->trigger= new EventObject("EventName", array("square", "pink"));
$this->Events->trigger($event);
return false; // don't render a view
}
public function callbackMethod($shape,EventObject $color$event) {
$params = $event->getParams();
echo $shape$params[0] . " " . $color$params[1];
}
}
?> |