I am a new in CakePHP and I am looking for some code, I have been downloaded function which looks like below:
$this->set(array('fruit' => 'orange', 'vegetable' => 'kale'));
In the code, the array variables are accessed in another controller functio开发者_如何学Cn using this method:
$varsSet = $this->viewVars;
echo $varsSet['vegetable'];
What I would like to do access the array variables in the same function in the controller where the $this-set() statement is made, and it seems like I should be able to do so with just one line of code. I have tried all of the following:
echo $fruit;
echo $this->field('fruit');
echo $this->MyModel->$fruit;
echo $this->MyModel->field('fruit');
And all of these throw parse, undefined variable, or variable not found errors. What would be the simplest/most proper way to access the variable within the same function in the controller?
Thanks,
Jonathan
Function $this->set() in the controller is used to pass variables from the controller to the view.
I.e.
If you have:
$this->set('fruit' => array('orange', 'vegetable' => 'kale'));
Then in the associated view you can access the array directly as
print_r($fruit);
If you want to use fruit variable in the controller, then you have to assign it to the var i.e.:
$fruits = array('orange', 'vegetable' => 'kale');
$this->set('fruit', $fruits);
But your question is not very clear what do you want to achieve with this.
精彩评论