The beforeFilter() method executes before any other method executes in the controller.
First use the Event namespace before defining the class in your controller file.
use Cake\Event\Event;
In your controller, add the beforeFilter() method as shown below.
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
}
Or, You can use the initialize()
method.
public function initialize(){
parent::initialize();
}