Friday, 17 February 2012

compact() or set multiple values in cake php

set() ,method is the main way to send data from your controller to your view. Once you
have used set(),the variable can be access in your view.

Syntex of set is-> "set('variable','value')"

In Cake-php's controller's file you set the variable for accessing in view's file as-

<?php
  $this->set('variable1','value1');
  $this->set('variable2','value2');
  $this->set('variable3','value3');
?>

There is another way to set multiple variables for the same page, By using "compact()" funtion.

In Cake-php's controller's file you write this as-

<?php
    $this->set(compact('variable1','variable2,''variable3'....));
?>

The compact() function creates an array from variables and their values.
And you can access these set variable as in view's file...

<?php
   echo $variable1;
   echo $variable2;
...........
?>




1 comment:

  1. Great and easy way of explanation. Please provide all cakephp component explanation in such a easy way. Thanks Atul

    ReplyDelete