Exception on beforeRender in AppController in cakephp 4.2

When I click on a link of my site it gives an exception of Undefined property: ErrorController::$Authentication
This is the code of beforeRender:


	public function beforeRender(\Cake\Event\EventInterface $event)
	{
		parent::beforeRender($event);
		$result = $this->Authentication->getResult();
		// If the user is logged in send them away.
		$this->loadModel('Controllers');
		$this->loadModel('GridOperations');
		$controller = $this->Controllers->find('all')->where(['name' => $this->request->getParam('controller')])->first();
		if($result->isValid() && !empty($controller)) {
			$user = $this->Authentication->getIdentity();
			$gridOperationsAdd = $this->GridOperations->find('all')->where(['profile_id'=>$user->profile_id, 'controller_id'=>$controller->id, 'sort_order' => 0])->first();
			$gridOperations = $this->GridOperations->find('all')->where(['profile_id'=>$user->profile_id, 'controller_id'=>$controller->id, 'sort_order >' => 0])->order(['sort_order' => 'ASC']);
			$countAdd = $this->GridOperations->find('all')->where(['profile_id'=>$user->profile_id, 'controller_id'=>$controller->id, 'sort_order' => 0])->count();
			$countOperations = $this->GridOperations->find('all')->count();
			$this->set('countAdd', $countAdd);
			$this->set('countOperations', $countOperations);
			$this->set(compact('gridOperationsAdd', 'gridOperations'));
		}
	}

How can I solve this problem?

I would guess its crashing on the 2nd line, but you can turn on debugging to see precisely where (assuming this site isn’t live - if so protect the debug switch by only granting it to your IP).

Did you bake in the Authentication?

Do you have:-

use Authentication\AuthenticationService;
use Authentication\AuthenticationServiceInterface;
use Authentication\AuthenticationServiceProviderInterface;
use Authentication\Middleware\AuthenticationMiddleware;

in your src/Application.php ?

Is there any reason you are using beforeRender() instead of beforeFilter() ?

Maybe its none of those things, but its a starting checklist!

The ErrorController does not load plugins or the like, in order to decrease the chance that it fails to report an error when the error is happening because of loading a plugin. This does mean that you can’t personalize your error pages. Your beforeRender will need to take this into account.

I solved it, I changed the name of a field and didn’t change in the code, I am now using beforeFilter