Passing parameters from the action to the view: Symfony vs Yii

This is the first post in the Symfony vs Yii series, that compare common functionalities between Symfony 1 and Yii, specifycally symfony 1.4.8 and Yii 1.1.5.

In the Model View Controller pattern, the controller needs to send information to the view, to be properly displayed to the user. This information is send in form of variables. Then, this time I will show how to pass custom variables from the controller (Yii) or action (symfony) to the view (Yii) or template (symfony).

To accomplish this task, there is two ways in Yii:

Yii way

As a parameter to the render method

In the protected/controllers/MessageController.php class:

 
class MessageController extends Controller
{ 
  public function actionHelloWorld()
  {
    $var = 'This is my test variable';
 
    $this->render('helloWorld', array('var' => $var));
  }
}
 

In the protected/views/helloWorld.php view file:

 
  ...
  ?php echo $var ?>
  ...
 

This will display:

 
  This is my test variable
 

As you can see, the second parameter of the controller’s render method contains the variables that will be available in the view file.

As a property of the Controller class

In the protected/controllers/MessageController.php class:

 
class MessageController extends Controller
{
  public
    $var;
 
  public function actionHelloWorld()
  {
    $this->var = 'This is my test variable';
 
    $this->render('helloWorld');
  }
}
 

In the protected/views/helloWorld.php view file:

 
  ...
  ?php echo $this->var ?>
  ...
 

This will display:

 
  This is my test variable
 

In Yii, the view file has access to the controller class through the $this variable, then, writing $this->var will retrieve the var property.

Symfony way

As a magic property of the controller class

In the apps/frontend/modules/Message/actions/actions.class.php class:

 
class MessageActions extends sfActions
{
  public function executeHelloWorld(sfWebRequest $request)
  {
    $this->var = 'This is my test variable';
  }}
 

In the apps/frontend/modules/Message/templates/helloWorldSuccess.php template file:

 
  ...
  ?php echo $var ?>
  ...
 

This will display:

 
  This is my test variable
 

As you can see, in Symfony there is no need to indicate the template, it defaults to the {action name}Success.php template file.

Now you should be asking, why do can be assigned an unexistent property. The answer is based on the magic methods __get, __set, __isset and __unset, implemented by sfComponent class, which is an ancestor in the inheritance hierarchy of the sfActions class.

In short words this __get and __set magic methods allow to assigned any unexistent property to the class.  Internally this properties are saved in a varHolder property which can be considered as an associative array. Then this array is extracted to the template file. This way you can use your previous assigned “properties” in the template file.

If someone ask me, I prefer the Symfony way because you have to write a little less code, but again this is my opinion.

Every MVC framework must implement a way to pass variables from the controller to the view, in this case it was showed how to do this with two awesome PHP frameworks: Symfony and Yii.

Leave a Reply

Your email address will not be published. Required fields are marked *