How to declare beforerender method in Cakephp 4.x

I am declaring the method as follows.

public function beforeRender(EventInterface $event)

It is showing the following error

Declaration of App\Controller\AppController::beforeRender(App\Controller\EventInterface $event) should be compatible with Cake\Controller\Controller::beforeRender(Cake\Event\EventInterface $event)

Is there a problem when declaring the function? If so what?

It looks like it’s applying the wrong EventInterface, perhaps drawn from the order of your uses at the top. If you hardcode the full path to the class its expecting it should be good. Something like

public function beforeRender(Cake\Event\EventInterface $event)
1 Like

Almost certainly, you just haven’t included

use Cake\Event\EventInterface;

at the top of your controller file, so when you say “EventInterface” in the function declaration, it is assuming it’s in the current namespace, i.e. App\Controller.

1 Like