Custom

Any class can be used as a Crud Listener, even the controller.

Using a controller as a listener

We override the implementedEvents() method in the controller, and bind the Crud.beforeFind event to the _beforeFind() method in the controller.

<?php
namespace App\Controller;

class BlogsController extends AppController {

  public function implementedEvents()
  {
      return parent::implementedEvents() + [
          'Crud.beforeFind' => '_beforeFind'
      ];
  }

  public function _beforeFind(\Cake\Event\Event $event, \Cake\ORM\Query $query)
  {

  }

}

Creating a listener class

Creating your own listener class is very similar to using a controller as a listener.

<?php
namespace App\Lib\Listeners;

use Cake\Event\Event;
use Crud\Listener\BaseListener;

class MyListener extends BaseListener
{
    public function implementedEvents()
    {
        return [
            'Crud.beforeFind' => '_beforeFind'
        ];
    }

    public function _beforeFind(Event $event)
    {
        Log::debug('Inside the listener!');
    }
}
  v: 4.4.2
Versions
latest
stable
4.4.2
4.4.1
4.4.0
4.3.5
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.4
4.1.3
v4.0.0
cake3
Downloads
On Read the Docs
Project Home
Builds
Downloads
On GitHub
View
Edit

Free document hosting provided by Read the Docs.