我有一个使用CRUD插件的Cakephp 3.x API。这是我的crud配置:
$this->loadComponent('Crud.Crud', [
'actions' => [
'Crud.Index',
'Crud.View',
'Crud.Add',
'Crud.Edit',
'Crud.Delete'
],
'listeners' => [
'Crud.Api',
'Crud.ApiPagination',
'Crud.ApiQueryLog'
]
]);
在我的控制器中,当我调用this-> Crud-> implementedEvents()时,它返回beforeFilter startuperxot beforePaginate only
public function index(){
$this->Crud->implementedEvents(); //returns beforeFilter startuperxot beforePaginate
$this->Crud->on('beforeFind', function(\Cake\Event\Event $event) {
if(isset($this->request->query['state'])){
$event->getSubject()->query->where(['state =' => $this->request->query['state']]);
}
});
return $this->Crud->execute();
}
如何启用beforeFind监听器?提前致谢。
你应该在索引上使用beforePaginate
https://crud.readthedocs.io/en/latest/actions/index.html#crud-beforepaginate