Yii framework - CHtml::link() example  some spaghetti code
In this post, i try to make some spaghetti code for Yii Chtml::link() method, to get familiar with Yii Chtml::link() fast. It's may be helpful for some one who is new in Yii.
     /**
       link() method
     */

     public static string link(string $text, 
                                      mixed $url='#', 
                                      array $htmlOptions=array ( ))

     /**
       @params: $text: anchord text (also called link text, link title)
                $url : value of href attribute
                $htmlOptions : array of other html attributes
    */
    

Basic using of CHtml::link()

echo CHtml::link('Link Text',array('controller/action')); 
Basing on your urlManager config, the output href may be different, but will be like the following in HTML output:
<a href="index.php?r=controller/action">Link Text</a>
or
<a href="controller/action">Link Text</a>

2. Link to an action with parameter

Sometime, we need to include some parameter when sending request to an url. To do that, you just have to add more element to url array like:
echo CHtml::link('Link Text',array('controller/action',
                                         'param1'=>'value1',
                                         'param2'=>'value2')); 
It's will include all parameters in url like
<a href="index.php?r=controller/action&param1=value1&param2=value2">
Link Text</a>

3. Link to an action have the same controller

echo CHtml::link('Link Text',array('action')); 

4.Link to an action in specific module

For controller in the same module, you also declare in this way to set the absolute path link to controller.
echo CHtml::link('Link Text',array('/module-name/controller/action')); 

5. Link to an action in root

With slash ('/') at the beginning of url array, it's set the absolute path for url
echo CHtml::link('Link Text',array('/controller/action')); 

Customizing $htmlOptions with CHtml::link()

6.Open link in new tab

 echo CHtml::link('Link Text',array('controller/action',
                   'param1'=>'value1'), array('target'=>'_blank');

7. Add class and id for link

 echo CHtml::link('Link Text',array('controller/action',
                   'param1'=>'value1'), 
                    array('class'=>'class_name','id'=>'id_name');

Linking to a controller action via POST

- Delete via Post method, by clicking on link :
    echo CHtml::link('Delete',
                     "#",
                     array("submit"=>array('delete', 'id'=>$data->ID), 
                           'confirm' => 'Are you sure?')); 
Reference:
  •  http://www.yiiframework.com/wiki/48/by-example-chtml/
  • http://www.yiiframework.com/doc/api/1.1/CHtml#link-detail