1.

What Are The Advantages Of Each?which Would You Use And Why?

Answer»

An advantage with FIRST case $this->set('posts', $posts); is that it allows two different names for the view file and controller file. For example, you could write SOMETHING like $this->set('postData', $posts);. Now the variable name in the view file would be $postData.

The advantage with the second approach $this->set(compact()); is easier to write, and USEFUL especially when we are setting several variables to the view.No NEED to add separate line for each variable as we have with $this->set();

For example,

$this->set(compact('posts','users','reports'));

An advantage with first case $this->set('posts', $posts); is that it allows two different names for the view file and controller file. For example, you could write something like $this->set('postData', $posts);. Now the variable name in the view file would be $postData.

The advantage with the second approach $this->set(compact()); is easier to write, and useful especially when we are setting several variables to the view.No need to add separate line for each variable as we have with $this->set();

For example,

$this->set(compact('posts','users','reports'));



Discussion

No Comment Found