Sunday, March 9, 2014

Yii-call-function-from-other-class

In this tutorial i will show how to call a method from a class

first we should import the class.

after that we should instantiate the imported class.

then we can call method via instance.

Example,


class AnotherController extends Controller
{

public function __construct($id=NULL)
{

}

public function message()
{
return 'Message from Another Controller';
}
}

?>

Above lines shows a class(Controller). assume that this class is placed in side

the contollers folder.

we want to access method "message" from another controller.

solutioin is ,

public function messageFromAnotherClass()
      {
     
  Yii::import("application.controllers.AnotherController");
  $another = new AnotherController();  
  return $message = $another->message();
      }

yii-create-cutom-application-component


In this tutoial , i will show you how to create a simple custom Application Component.

First of all create a file named simple with following codes and put the file in the path

protected/components/Simple.php


class Simple  extends CApplicationComponent
{

    public function init()
    {
echo 'Simple Initiated
';
    }

    public function message()
    {
      return 'Message from Simple';
    }
   
   
}


?>

do the following changes int the main.php under config folder

'components'=>array(
........
            'simple'=>array('class'=>'Simple'),
.........

),


Now its time to call method in the created component.

steps are,

echo Yii::app()->simple->message();


output is

Simple Initiated
Message from Simple

Thursday, March 6, 2014

Yii-cgridview-table-css-class

By default Yii Cgridview have a spcific CSS formatting in gridview's table.

But we can apply css table class whatever we want to display.

we can apply it by htmlOptions array.

Example,

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'account-grid',
'dataProvider'=>$model->search(),
 'htmlOptions' => array('class' => 'table table-hover table-striped table-bordered table-condensed'),
'filter'=>$model,
'columns'=>array(
'id',
'account_name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>