Events are the backbone of Crud, and your primary gateway into customization of Crud and fitting it to your applications.
You can subscribe to events from almost everywhere, and in multiple ways.
We override the implementedEvents()
method in the controller, and bind the Crud.beforeFind
event to the _beforeFind()
method in the controller.
When using this technique, you need to prefix all the event names with Crud.
Most of the other ways to listen for events do not need this, as it's done automatically.
namespace app\Controller;
class BlogsController extends AppController
{
public function implementedEvents()
{
return parent::implementedEvents() + ['Crud.beforeFind' => '_beforeFind'];
}
public function _beforeFind(\Cake\Event\Event $event)
{
}
}
Note
It's important that the controller event method is public
, since it's called from the CakePHP event manager, outside of the Controller scope.
The added _
prefix is there only to prevent it being executed as an controller action.
You can bind events directly in your controller actions, simply call the on()
method in Crud and provide a callback. The example below uses a closure
for the callback, but everything that is valid for call_user_func()
can be used
public function view($id)
{
$this->Crud->on('beforeFind', function(\Cake\Event\Event $event) {
// Will only execute for the view() action
});
return $this->Crud->execute();
}
Note
When implementing events in your controller actions, it's important to include return $this->Crud->execute();
otherwise Crud will not process the action.
The benefit of the controller method is that you can easily share it between two actions, like below.
public function view($id)
{
$this->Crud->on('beforeFind', [$this, '_beforeFind']);
return $this->Crud->execute();
}
public function admin_view($id)
{
$this->Crud->on('beforeFind', [$this, '_beforeFind']);
return $this->Crud->execute();
}
public function _beforeFind(\Cake\Event\Event $event)
{
// Will execute for both view() and admin_view()
}
Different Crud actions <actions>
will emit a different combination of events during their execution, with different Subject <crud-subject>
data. If you are looking for events specific to an action, check the specific Crud action <actions>
documentation page.
This is a full list of all events emitted from Crud.