As it would happen, I just needed to change the variable name. I'm either hitting on a reserved keyword, or I've previously defined the variable in my beforeFilter method. Not sure which yet, but credit goes to scrowler for pointing that one out.
CakePHP - Render different view and set variables
-
19-06-2023 - |
Question
I am attempting to render and set variables to a view that does not belong to any controller. Here is an example of my folder structure:
app/
-->View/
-->ExternalReportViews/ (There is no ExternalReportViews Controller)
-->example_view.ctp
I am trying to pass a variable called $orders to the view, I have verified that this variable exists and contains data by debugging from the controller.
I have succesfully rendered the view using any of the following methods:
// Method 1
$this->render('/ExternalReportViews/example_view');
$this->set('orders', $orders);
// Method 2
$view = new View($this, false);
$view->viewPath = 'ExternalReportViews';
$view->render('example_view');
$view->set('orders', $orders);
// Method 3
$this->viewPath = 'ExternalReportViews';
$this->render('example_view');
$this->set('orders', $orders);
It seems that regardless of whether the set method is placed before or after the render in any of the above methods, the $orders variable is not passed to the rendered view.
When attempting to debug($orders) in the view, I see that the variable is undefined.
I could obviously bypass this problem by temporarily storing $orders in $this->Session, but that seems a bit messy.
The reason for all of this is that I will have a number of views that will be created on a per-report basis, and I would rather have them in a separate (sub)folder(s) for cleaner file management.
Solution