1

I'm trying to understand how Yii passes data from page to page. One thing that confused me was that the view pages associated with their respective controller could access the data sent to it through render without a post or get request. I.e.

//in the controller php file
$this->render('view',array('data1'=>$data1))) 

//in the view php file
if (isset($data1)) { //do something amazing }

Now I realize it's because the view will have access to the members of the controller. But then, if that's the case, why would anyone bother putting a data array in the render function?

Consider another example provided on SO here.

What am I misunderstanding?

Community
  • 1
  • 1
Klik
  • 1,757
  • 1
  • 21
  • 38
  • That's not sending anything "from page to page". It's merely passing data from the controller to the view template. It has nothing to do with GET or POST. All this is happening server side; GET or POST are client<->server interactions. – deceze Jan 15 '15 at 03:47
  • The answer you gave here (http://stackoverflow.com/a/6327314/1406888) talks about how PHP is stateless. Which is true most of the time in Yii framework (please correct me if I'm wrong I'm still really new at Yii). But here the data persisted because it's still encapsulated inside the class. Is this true? If that's the case, why would anyone bother using a parameter with render() instead of including important data as a class member? – Klik Jan 15 '15 at 03:59
  • You really seem to be confusing ***internal*** passing of data between functions/components and data passing from request to request. A PHP request works like: 1) client sends GET/POST HTTP request to server, 2) server does something, 3) server returns HTML HTTP response, 4) repeat from 1). The code you're showing is all happening in step 2) here. There's no cross-request "persistence" or GET/POST involved. – deceze Jan 15 '15 at 04:02
  • What I mean to ask is, won't it always be the case that you can always get around sending an array to render since during the same request it will have access to its controller's members? – Klik Jan 15 '15 at 04:07

1 Answers1

2

The point of the data array parameter is to be able to pass variables to the view file that are not properties of the controller. If your controller code is, say, this:

/* controller */

$this->property = 'Controller Property';
$variable = 'Method-scoped variable';

$this->render( 'view' ); // no second parameter

Then $variable would not be available in your view code:

/* view */

echo $this->property; // "Controller Property"
echo $variable; // null;

By passing an array to the render method, you can have it extract the array members into variables available in the view script:

/* controller */
//...

$this->render( 'view', array( 'myvar' => $variable ) );

/* view */

echo $myvar; // "Method-scoped variable"

Unless your views are only going to use controller properties in the render, you're going to need to use the second parameter to pass on scoped information so it's available to the view script.

Brian North
  • 1,398
  • 1
  • 14
  • 19
  • That makes so much sense. I've been staring at this Yii framework for too long. You have a well explained answer and it answers my question perfectly. A bonus question if you would; Besides the online docs is there any really amazing resource to draw out the program flow for a newbie like me? – Klik Jan 15 '15 at 04:59
  • Unfortunately I wouldn't be the right person to ask about beginner tutorials, as I *don't actually use Yii*. I was able to answer based off this being a common programming pattern and a double-check with the online docs. I did notice the Yii framework site has an extensive list of [tutorials](http://www.yiiframework.com/tutorials/), though. – Brian North Jan 15 '15 at 20:24