0

So we are running into a problem that is recurring several times, caught through logs. We are getting loads of undefined variable notices. When we trace where they are supposedly undefined, they are always defined in the matching controller action to the view where it is complaining. We can't figure out how someone is getting to the view either without hitting the controller action, or with hitting it but losing variable definitions somehow.

We tried logging the controller and action whenever a view does this (by checking for undefined vars at thee top of the view), and it always says the correct controller action, so we don't have any unexpected controller actions where the variables aren't defined. It's problematic partially because it's filling up our logs, but we also worry actual users may be getting unexpected results. Does anyone have any idea of a scenario that could cause this?

It has happened with several views that do work fine when tested on our end (we've had several people hit the appropriate urls and the problem doesn't happen). We also checked with a code search, and no other controllers render these views. We have basic safety checks going on for when certain variables are expected in the url; for example, if an id is missing, it will throw a not found exception rather than trying to continue, so we know that's not the cause either.

  • A short and relevant excerpt from where you define them in the controller code and where you access them in the view code might help us debug your issue. –  Aug 03 '20 at 19:10
  • Example: in controller $variable = 'thing'; $this->set(compact('variable')); Example in view echo $variable It's really that simple. We have all kinds of ways of defining variable but even one this simple will show up as undefined. We know they work normally because upon visiting the url, the variables ARE defined. So the problem makes no sense. – Jenny Chalek Aug 03 '20 at 19:22
  • I'm confused why you expect a scoped variable to be shared between two separate scopes. [This page](https://book.cakephp.org/4/en/controllers.html#setting-view-variables) on CakePHP's website may be useful—it appears that you need to use `Controller::set()` to send data from the controller to the view. –  Aug 03 '20 at 19:26
  • That's what we are doing with $this->set. When you're in the controller, you use $this->, not Controller::. There is nothing wrong with our code. It works under all circumstances we've been able to come up with. It just occasionally apparently doesn't work somehow under some circumstance we haven't been able to recreate. – Jenny Chalek Aug 03 '20 at 20:06

0 Answers0