Tip
While CRUD provides many listeners, it’s definitely possible and recommended that you add your own reusable listeners for your application needs
Listeners are the foundation for the extreme flexibility CRUD provides you as an application developer.
The event system allows you to hook into the most important part of the CRUD action flow and customize it to your unique application needs.
The listener system is simply the Events System from CakePHP, and all the official documentation and usage also applies to CRUD.
The CRUD event system uses two methods trigger()
and on()
to interface
the underlying CakePHP event system.
The only hard requirement for a CRUD listener is that it needs to either implement
the implementedEvents()
method or extend \Crud\Listener\Base
.
Below is the code for a simple CRUD listener.
In the next few sections we will walk through the code and explain how it works, and what every single line of code does.
For each section, the relevant lines of code will be highlighted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php
namespace Crud\Listener;
class Example extends \Crud\Listener\BaseListener {
/**
* Returns a list of all events that will fire in the lister during the
* CRUD life-cycle.
*
* @return array
*/
public function implementedEvents() {
return [
'Crud.beforeRender' => ['callable' => 'beforeRender']
];
}
/**
* Executed when Crud.beforeRender is emitted
*
* @param \Cake\Event\Event $event
* @return void
*/
public function beforeRender(Cake\Event\Event $event) {
$this->_response()->header('X-Powered-By', 'CRUD 4.0');
}
}
|
All built-in listeners in CRUD live in the Crud\Listener
namespace.
All listeners in CRUD, even your own, should inherit from the
Crud\Listener\Base
class.
This class is abstract
and provides numerous auxiliary methods which can be
useful for you both as a developer and as an action creator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php
namespace Crud\Listener;
class Example extends \Crud\Listener\BaseListener {
/**
* Returns a list of all events that will fire in the lister during the
* CRUD life-cycle.
*
* @return array
*/
public function implementedEvents() {
return [
'Crud.beforeRender' => ['callable' => 'beforeRender']
];
}
/**
* Executed when Crud.beforeRender is emitted
*
* @param \Cake\Event\Event $event
* @return void
*/
public function beforeRender(Cake\Event\Event $event) {
$this->_response()->header('X-Powered-By', 'CRUD 4.0');
}
}
|
As documented in the CakePHP Events System
all listeners must contain a implementedEvents
method.
In this example, we simply request that beforeRender
in our class is executed
every time a Crud.beforeRender
event is emitted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php
namespace Crud\Listener;
class Example extends \Crud\Listener\BaseListener {
/**
* Returns a list of all events that will fire in the lister during the
* CRUD life-cycle.
*
* @return array
*/
public function implementedEvents() {
return [
'Crud.beforeRender' => ['callable' => 'beforeRender']
];
}
/**
* Executed when Crud.beforeRender is emitted
*
* @param \Cake\Event\Event $event
* @return void
*/
public function beforeRender(Cake\Event\Event $event) {
$this->_response()->header('X-Powered-By', 'CRUD 4.0');
}
}
|
Note
The Crud.beforeRender
event is similar to the Controller and View event of the
same name, but Crud.beforeRender
is called first, and can halt the entire
rendering process
This method gets executed every time a Crud.beforeRender
event is emitted
from within CRUD or by you as a developer.
When the event is emitted, we append a header to the client HTTP response named
X-Powered-By
with the value CRUD 4.0
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php
namespace Crud\Listener;
class Example extends \Crud\Listener\BaseListener {
/**
* Returns a list of all events that will fire in the lister during the
* CRUD life-cycle.
*
* @return array
*/
public function implementedEvents() {
return [
'Crud.beforeRender' => ['callable' => 'beforeRender']
];
}
/**
* Executed when Crud.beforeRender is emitted
*
* @param \Cake\Event\Event $event
* @return void
*/
public function beforeRender(Cake\Event\Event $event) {
$this->_response()->header('X-Powered-By', 'CRUD 4.0');
}
}
|