Jump to content
Larry Ullman's Book Forums

Which View Variables Are Passed?


Recommended Posts

version 0.5

 

Page 113

 

Quoting from the book:

 

 

"The view files generated by Gii have comments at the top of them that indicate the variables that were passed to the view file.
 
For protected/views/site/index.php, that’s:
<?php
/* @var $this SiteController */
 
For protected/views/user/create.php, you’ll see:
<?php
/* @var $this UserController */
/* @var $model User */  "

 

My question: What actual variables are passed? Is SiteController a variable? Is UserController a variable? What exactly does "$model User" mean? 

 

Thanks.

Link to post
Share on other sites

This is the MVC pattern at work. All these are variables that are passed to the view by a controller action. These variables holds objects, where $this in both cases points to a controller object. $model is the User object. You have to look at the actual controller and models classes to determine what methods are available to you.

 

You should really use time to revisit the introduction on MVC. It's one of the keys to understanding YII. :)

Link to post
Share on other sites

Why is "$this" necessary? There is only one SiteController.php. 

 

Why is the syntax not   $this->SiteController  and   $model->User  ?

 

And why does "@" always precede "var"?

 

Thanks.

 

Reference:

"The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object)."

 

From http://php.net/manual/en/language.oop5.basic.php

Link to post
Share on other sites

$this is just a variable. It could be $anything. It depends in how you pass on the variable from the controller to the view. Just names.

 

Because it's not $this as inside the class itself. It's confusing, but as it's the controllers own object you pass along, you often call it "$this". In the end, it's just a name. Change it to $object or something else if that makes more sense to you.

 

@ Always preceds because of something called PhpDoc. It's a documenting syntax that widely recognized, as it's build on JavaDoc. It makes more sense when a coding software often called an IDE can utilize the documentation to give you help as you write code. "@return SiteController $this" could be used in a method inside the SiteController that uses "return $this; This is not critical by any stretch of the imagination as PHPDoc is not syntax checked. It helps you out understanding a class quickly, nothing else.

 

That reference is the essence of what I wrote above. As it's $this in a view only means the SiteController passes it's own instance object through to the view. This can be complicated to grasp, but It's really only an object that can be used exactly like $user.

 

Hope that clarifies. Please ask if you need more man.

Link to post
Share on other sites

I think you are saying that

 

1. "@" is just a documenting convention, and

2.  "$this" is necessary to indicate a particular instance of SiteController

 

But why is the syntax not   $this->SiteController  and   $model->User  ?

I mean, why is the "->" omitted?

 

 

Thanks.

Link to post
Share on other sites

Antonio is mostly correct here, except for what $this is. $this is a special variable that refers to the current object. In the view, $this is still doing that same thing. It's not actively being passed to the view from the controller like the other variables. $this is available within the view files, and has the value of the current controller, because the controller is rendering the view. Think of the view like being in a controller method.

  • Upvote 1
Link to post
Share on other sites

Because -> is the object operator, used to access object members. What you're looking at is documentation, not code:

 

/* @var $this UserController */
/* @var $model User */  "
 
$this is a variable of type UserController.
$model is a variable of type User.
Link to post
Share on other sites
I think I understand, but to be sure:

 

$this UserController" uses the term $this, whereas "$model User" does not.

 

The distinction is that "$this User" might mean "the current user accessing the site", whereas "$model User" means the instance of $model that contains the currently selected user.

 

Have I gotten it right yet?

 

I get the impression this is only an informal documentation syntax, without clearly defined rules. Is this true?
Link to post
Share on other sites

 

I think I understand, but to be sure:
 
$this UserController" uses the term $this, whereas "$model User" does not.
 
The distinction is that "$this User" might mean "the current user accessing the site", whereas "$model User" means the instance of $model that contains the currently selected user.
 
Have I gotten it right yet?

 

No, not quite there yet. "$this UserController" uses the term "$this" because the view is being rendered by a controller, which is represented by an object instance, and $this always refers to the current object (within the object). $this is always a special variable.

 

"$model User" is just a different variable, of type User. 

 

"$this User" will never represent the current user accessing the site. "$model User" represents a retrieved record from the "user" table. 

 

 

 
I get the impression this is only an informal documentation syntax, without clearly defined rules. Is this true?

 

No, actually quite the opposite. This is a very formal documentation syntax, with clearly defined rules. As Antonio said earlier, this is PHPDoc syntax, which is based upon JavaDoc. You can research either to learn more about the syntax and rules.

Link to post
Share on other sites

In Object-oriented programming, class context is very important. I miss led you a bit in my last post. $this is a special variable that can be translated into "this object instance", a.k.a "this object itself". It's important to notice that the view is rendered as part of the SiteController object, so $this will always point to the concrete controller that is ran. To get this, it's very important to understand the bootstrapping/routing part of a framework. This is why I've told you the basics are so damn important. ;)

 

$model is a Model object of type User. You need to understand that Models are something different from components. YII does also have a component called CWebUser. THAT component creates an object that let's you interact with a specific instance of a user. That is however not tied to a model.

 

$user = Yii::app()->user;

Gives you the current user. You need to look at CWebUser to understand how to play with that.

 

About the PHPDoc. It does absolutely have a formal rule set, but It's not code. You'll therefor not get parse errors, or anything similar in you don't follow conventions. Following the rules will allow you to get a quick overview of a method in an IDE.

 

Class method:

1418903.jpeg

 

Method call:

1418904.jpeg

Link to post
Share on other sites

Larry, from what you have written, I understand that protected/views/user/create.php is being rendered by an instance of UserController. That instance of UserController is the current object. It is referred to by $this -- even within the view file. 

 

However, I do not know what general rule would tell me that the UserController instance is the current object.

 

If while coding at some future time, I am not sure what the current object is, I suppose I could type "echo get_class($this);". Does that make sense?

Link to post
Share on other sites

The UserController instance is the current object:

 

- In the UserController class

- In views rendered by the UserController class

 

In general, both will be the case when the route (i.e., the URL) is of the format ...index.php/user

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...