程序员

createUrl和CHtml::link参数设置的区别

2016-04-01  本文已影响274人  IT小C

createUrl将多个参数放进一个数组中,CHtml::link直接向后累计添加(不放进数组)
1.createUrl和createAbsoluteUrl
createUrl生成的是相对路径
createAbsoluteUrl生成的是绝对路径

在MVC中

$this->createUrl

在Yii中使用

Yii::app()->createUrl

<?php
    $companyNew = (object)[
        'id'=>'1',
        'news_type'=>'boy',
        'title' => '123'
    ];  
?>
<a href="<?php echo $this->createUrl('controller/login',array('id'=>$companyNew->id,'news_type'=>$companyNew->news_type)) ?>" title="<?php echo $companyNew->title ?>">链接</a>

生成的html

<a href="/yii7/demos/test/index.php?r=controller/login&id=1&news_type=boy" title="123">链接</a>

2.CHtml::link

<?php echo CHtml::link('链接',array('controller/login','p'=>'cc','q'=>'dd'),array('target'=>'_blank'));?> 

生成的html

<a target="_blank" href="/yii7/demos/test/index.php?r=controller/login&p=cc&q=dd">链接</a>

3.CHtml::linkButton

<?php echo CHtml::linkButton('LinkName', array('submit'=>array('controller/action','param'=>'value1'), 'confirm'=>"Are you sure?",));?> 

生成的html

<a href="#" id="yt0">LinkName</a>
/*<![CDATA[*/
jQuery(function($) {
  jQuery('body').on('click','#yt0',function(){if(confirm('Are you sure?')) {jQuery.yii.submitForm(this,'/yii7/demos/test/index.php?r=controller/action&param=value1',{});return false;} else return false;});
});
/*]]>*/
上一篇 下一篇

猜你喜欢

热点阅读