createUrl() and CHtml::link()

Hello guys!

I’ve been seeing some things about CHtml::link() and createUrl(), and by what I saw through the API CHtml::link(), creates links using the createUrl() through the normalizeUrl().

So, It is not enough just use CHtml::link() to create links and/or urls? Another question, when we must use the CHtml::link() or the createUrl()? What is the most important argument for choosing one or other?

Another thing that I have seen some people use is:

echo CHtml::link('Test', $this->createUrl('controller/action', 'id'=>10));

Okay! Why to use the ‘$this->createUrl(…)’ if the CHtml::link() already uses it by default? Doing

echo CHtml::link('Test', array('controller/action', 'id'=>10)); 

we will not get the same result?


The method CHtml::link is going to call normalizeUrl only when its second parameter is passed as an array.

CHtml::normalizeUrl converts array format into string format.

The string resulting from CController::createUrl(wrapper around Yii::app()->createUrl) or string containing some external link is not going to invoke CHtml::normalizeUrl.

So if you are passing CController::createUrl as a second parameter to CHtml::link, it means that you yourself normalizing the url.

Alright, thank you for the answer… but what about my 2nd question? When we should use the CHtml::link() or the createUrl()?


CController::createUrl/CHtml::normalizeUrl/Yii::app()::createUrl/CUrlManager::createUrl - All these methods we internally use inside the code for which end user has no knowledge about it.

CHtml::link method is helpful in creating visible links on pages.