Post date: Apr 10, 2014 3:45:27 AM
Example 1: Linking to a controller action
<?php echo CHtml::link('Link Text',array('controller/action')); ?>
HTML Output:
<a href="index.php?r=controller/action">Link Text</a>
Example 2: Linking to a controller action with querystring parameters
<?php echo CHtml::link('Link Text',array('controller/action', 'param1'=>'value1')); ?>
HTML Output:
<a href="index.php?r=controller/action¶m1=value1">Link Text</a>
Example 3: Linking to a controller action with multiple querystring parameters
<?php echo CHtml::link('Link Text',array('controller/action', 'param1'=>'value1', 'param2'=>'value2', 'param3'=>'value3')); ?>
HTML Output:
<a href="index.php?r=controller/action¶m1=value1¶m2=value2¶m3=value3">Link Text</a>
Example 4: Link opening a new page
<?php echo CHtml::link('Link Text',array('controller/action', 'param1'=>'value1'), array('target'=>'_blank'); ?>
HTML Output:
<a target="_blank" href="index.php?r=controller/action¶m1=value1">Link Text</a>
Example 5: Linking to a controller action inside the actual controller (Suppose you are in the PostController/view and wants to link to PostController/create)
Just remove the 'controller' part from the string
<?php echo CHtml::link('Link Text',array('action')); ?>
If you are linking to an action from another controller, use the syntax of the former examples.
Example 6: Linking to a controller action from the site root (Suppose you are inside a module and wants to make the link from a controller of the root application)
In this case, add an slash "/" at the start of the string url
<?php echo CHtml::link('Link Text',array('/controller/action')); ?>
This makes more sense if you are working with modules.
Example 7: Linking to a controller action from another module
Replace below the module-id with desired module id .
<?php echo CHtml::link('Link Text',array('/module-id/controller/action')); ?>
Example 8: Linking to a controller action from the same module
This is useful when you want to make absolute paths avoiding to use static module names.
<?php echo CHtml::link('Link Text',array('/{$this->module->id}/controller/action')); ?>
Example 9: Linking to a controller action via POST with confirmation dialog
Delete actions created using gii require the delete request be sent via POST to help prevent deleting objects by accident. Below is an example how to create a link that sends the request via POST and also asks for confirmation. Where you are redirected after the delete depends on your delete action. Note that the id link parameter below is a GET type parameter (submit URL will be something likehttp://example.com/post/delete/id/100).
<?php echo CHtml::link('Delete',"#", array("submit"=>array('delete', 'id'=>$data->ID), 'confirm' => 'Are you sure?')); ?>
If you are using CSRF protection in your application do not forget to add csrf parameter to the htmlOptions array.
<?php echo CHtml::link('Delete',"#", array("submit"=>array('delete', 'id'=>$data->ID), 'confirm' => 'Are you sure?', 'csrf'=>true)); ?>
Example 10: Linking to a controller action via POST with POST parameters
If you need to make POST request with arbitary link with additional POST parameters you should use following code (submit URL will be something like http://example.com/blog/deletePost/param/100).
<p><?php echo lnk('Delete blog post', '#', array( 'submit'=>array('blog/deletePost', 'param'=>100), 'params'=>array('id'=>$post->id, 'status'=>Post::STATUS_DELETED_BY_OWNER), 'csrf'=>true, )); ?></p>
Syntax:
public static string ajaxLink(string $text, mixed $url, array $ajaxOptions=array ( ), array $htmlOptions=array ( ))
Default example
echo CHtml::ajaxLink( $text = 'Click me', $url = '/', $ajaxOptions=array ( 'type'=>'POST', 'dataType'=>'json', 'success'=>'function(html){ jQuery("#your_id").html(html); }' ), $htmlOptions=array () );
Example #1 : Ajax request using ajaxLink
//In view: echo CHtml::ajaxLink( 'Test request', // the link body (it will NOT be HTML-encoded.) array('ajax/reqTest01'), // the URL for the AJAX request. If empty, it is assumed to be the current URL. array( 'update'=>'#req_res' ) ); echo '<div id="req_res">...</div>'; //In controller public function actionReqTest01() { echo date('H:i:s'); Yii::app()->end(); }
Example #2 : Ajax request using ajaxLink with loading image
//In view: echo CHtml::ajaxLink( 'Test request', // the link body (it will NOT be HTML-encoded.) array('ajax/reqTest01Loading'), // the URL for the AJAX request. If empty, it is assumed to be the current URL. array( 'update'=>'#req_res_loading', 'beforeSend' => 'function() { $("#maindiv").addClass("loading"); }', 'complete' => 'function() { $("#maindiv").removeClass("loading"); }', ) ); echo '<div id="req_res_loading">...</div>'; //In controller: public function actionReqTest01Loading() { sleep(4); // Sleep for 4 seconds just to demonstrate the Loading Image can be seen, for learning purpose only echo date('H:i:s'); Yii::app()->end(); }