Can not access request object in component. $this->request is null in component after upgrade to cakephp 4.0

646 views Asked by At

How can I access request object and session data in my Component in cakephp 4.0?

I've got the following error Call to a member function getSession() on null after upgrade in the following code $session = $this->request->getSession(); in my component.

1

There are 1 answers

1
Anton Menshutin On

To access Controller from within a component one should use $this->getController() (https://book.cakephp.org/4/en/controllers/components.html#accessing-a-component-s-controller).

To access session you can use $session=$this->getController()->getRequest()->getSession().