Search code examples
phpyiiyii-chtml

Adding href to CHTML::image


I'm currently using an image in my navbar but I also want to make clicking this return to the home page.

I thought adding ,array('href' => 'imagepath.png') would work but had no luck so far and it seems to break the page!

this is the code I am trying to change.

 echo CHtml::image(Yii::app()->request->baseUrl.'imagepath'); 

Keep in mind I have tried

echo CHtml::image(Yii::app()->request->baseUrl.'imagepath',
array('href' => 'imagepath')); 

Also trying

echo CHtml::image(Yii::app()->request->baseUrl.'imagepath'); 
echo CHtml::link($image,array('baseurl/page','id'=>'page_id'));

Answer : To get an image to link back to the homepage or chosen baseurl.

$img= CHtml::image(Yii::app()->baseUrl.'/imagepath');
$link = CHtml::link($img, Yii::app()->request->baseUrl);
echo $link;

Solution

  • Try this

    $img= CHtml::image(Yii::app()->baseUrl.'/imagepath','altText',array('height'=>100,'width'=>100));
    $link = CHtml::link($img, array('baseurl/page','id'=>'page_id'));
    echo $link;