0
votes

I am setting a viewVar in a Plugin controller. Before rendering I debug($this->viewVars) to check that the relevant variable is in the array (it is).

The variable I am setting is a private class variable, but I also tried creating a local copy of it and using the set(compact()) form to set the variable.

So my problem is not syntactic.

When I debug the variable in my view I find that it is not defined. A variable set in my main application AppController is correctly passed to the view.

If I debug the viewVars at various points throughout the application I see that

  • the main App Controller sets a variable, which is present in my plugin AppController
  • at the end of my plugin Controller (not AppController) the viewVars shows my variable
  • it is gone in my view, leaving only the variable set in my application AppController

If I set the variable in my main AppController (before filter) then it is present in my final view output.

If I add a 'beforeRender' method to my plugin to debug($this->viewVars) it shows only the variable set up in the main AppController and not the one in the my plugin.

How can I get a variable from a plugin Controller to be set as a view variable?

1

1 Answers

0
votes

The problem lay in the sequence of events in the controller callbacks.

I was stupidly calling a render command in the plugin beforeFilter() method. This should have been done in the afterFilter() method. Moving it there fixed the issue.