A big shout-out to my favourite typist for showing me some of these gems, which I will gladly share with you
So, one of the things that often happens in CakePHP is that you will have multiple values to send to your view, so you might have code that looks like this:
1
2
3
4
$this->set('user', $this->User->read(null, $id));
$this->set('foo', $foo);
$this->set('bar', $bar);
$this->set('baz', $baz);
Solution one? Use compact() to pass all the variables to your view.
1
2
$user = $this->User->read(null, $id);
$this->set(compact('user', 'foo', 'bar', 'baz'));
What does the compact() function do? It takes the array you pass into it and looks for variables of the same name as the elements in that array. It then spits out an array of key => value pairs. So, one little trick with compact() means you only have to use one set statement. This works because all those $this->set() statements simply add those values to an array. But you already knew that, right?
The second solution is similar to sing the “chmod 777 firehose” to solve UNIX-based permissions problems.
1
2
$user = $this->User->read(null, $id);
$this->set(get_defined_vars());