Let me begin by saying that you should never do this. But that’s in a perfect world, and sometimes things conspire against you and force you to break the rules.

The situation I found myself in is that I have a helper to display a threaded navigation menu down the side of the page and expand the current selection. Much like the side of the CakePHP book. This has worked fine in the year that the site’s been live. However, now we’re adding more functionality, which means that some more items need conditionally adding into the menu at certain points. And those items are not pages.

There is no official way to access your view variables (defined with $this->set('myVar', 'foo'); from your controller) in your view helpers. And there’s probably a good reason for that, but heck, I’m on a deadline.

The code to do this is:

ClassRegistry::getObject('view')->viewVars['myVar'];

What this does it get the instance of the view class that’s stored in the Class Registry then access the viewVars directly.

Yes, it’s messy. No, you shouldn’t use it. But it does get the job done.