Help: Router::getRequest() and $this->request


I am working on upgrading our servers from Cakephp 3.5.12 to Cakephp 3.9.x. Our application uses jwt for authorization and the following was done in 3.5 to set jwt as an attribute to the request object itself so that it can be used across our application for internal purposes

While upgrading, I noticed about the immutable aspect of request object that was introduced in 3.6. I had made changes to our code to accommodate (adding the jwt attribute like $this->request = $this->request->withAttribute(‘jwt’, ‘token value’) based on the changes mentioned here.

My challenge is, in some parts of our code Router::getRequest() has been used to get the jwt token (in a few model functions). Looking into the ActionDispatcher logic, I figured out that Router::pushRequest() was done first and then controller object is created and they are independent of each other.

I think the solution here, is to use $this->request to get the mutated request object. I would like to know if there is any other way to properly solve this.

Apologies if i have overlooked anything in this regard.


You should be passing the necesary info as SaveOptions. (or findOptions if you are using a find)

$this->Users->save($user, ['request' => $this->request]);
$this->Users->find('active', ['request' => $this->request]);

Then in the model read it from the $options parameter

Thanks. Will try it out.