Hi,
I would like to pass a variable from one controller function to an other. In other words, how can I access the variable from an other function, within the same controller?
Thanks
Hi,
I would like to pass a variable from one controller function to an other. In other words, how can I access the variable from an other function, within the same controller?
Thanks
Consdidering your Controllers are classes, you have two solutions :
Which one of those solutions should you use ?
I suppose it depends on the situation :
As Pascal mentioned, one way is to set a property on the object:
class CategoriesController extends AppController
{
public $foo = '';
public function index()
{
$this->foo = 'bar';
}
public function view($id = null)
{
$baz = $this->foo;
$this->set('baz', $baz);
}
}
Or pass it as argument:
class CategoriesController extends AppController
{
public function index()
{
$foo = "bar";
$this->view($foo)
}
public function view($param)
{
$this->set('bar', $param);
}
}
I noticed that defining a property in the controller is not persistent after subsequent calls to the controller.
However, defining a property in the model is persistent between controller function calls.