Friday, May 30, 2014

Yii: Step by Step Create Custom Component

Setup Component:

1. Create new class file in protected/components/Beauty.php
2. Start with the following code in Beauty.php

<?php
class Beauty extends CApplicationComponent
{
     public function makeup(){
        $colorbox = 'blue';
        return $colorbox;
     }
}
?>

3. Go to protected/config/main.php and add the following line under components:

'components'=>array(
   'beauty' = array (
            'class' => 'application.components.Beauty';
              ),
),


How to use it in controller:

1. In controller file:

<?php
  class FashionController extends Controller{
        //call beauty component;
       $beauty = Yii::app()->beauty;
       $color = $beauty->makeup();
       
        // output is blue;
        echo $color
  }
?>

No comments:

Post a Comment