What is the benefit of using from CHtml class?

[size="3"]what is difference between two bellow Line?

[color="#FF0000"]1)[/color]<a href="/demo/index.php?r=message/goodbye">Goodbye</a>

[color="#FF0000"]2)[/color]<?php echo CHtml:link(“Goodbye”,array(‘message/goodbye’));

which other is better,why?[/size]

I can’t explain because i bad english.

2 => depending on the router

view doc here: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls

With code:

echo CHtml:link("Goodbye",array('message/goodbye','id' => 1));


Route 1:

'rewrited-message/rewrited-goodbye/<id:([0-9]+)>' => 'message/goodbye'

Output Route 1:

<a href="index.php/rewrited-message/rewrited-goodbye/1">Goodbye</a>

Route 2:

'rewrited-goodbye/rewrited-message/<id:([0-9]+)>' => 'message/goodbye'

Output Route 2:

<a href="index.php/rewrited-goodbye/rewrited-message/1">Goodbye</a>

I agree with kjra1707, the benefit of using the CHtml::link allows you to change the format of URLs without having to go around changing all your code.

CHtml is a static class that provides a collection of helper methods for creating HTML views.

Nearly all of the methods in this class allow setting additional html attributes for the html tags they generate. You can specify for example. ‘class’, ‘style’ or ‘id’ for an html element. For example when using array(‘class’ => ‘my-class’, ‘target’ => ‘_blank’) as htmlOptions it will result in the html attributes rendered like this: class=“my-class” target="_blank".

if we will use CHtml::link(“Goodbye”,array(‘message/goodbye’)) than we can manage route in our yii project.

Personally i find it cleaner to use the CHtml and you can do lots of things easily like the url routing is also to your advantage. you also avoid concatenation which makes messy codes.

There is a huge difference between the lines you have written, and you should always use the second one. In the first case, you manually created <a href > with attribute href equal to /demo/index.php?r=message/goodbye.

In the second line, the link is generated automatically for you, based on the settings of the urlManager component. How can this help you?

Let’s say your boss is not very satisfied with the layout of your URLs and he wants more SEO frendly URLs. In first case, you will have to go through all view files where you have your links, and manually edit all links which is really hard and boring task if you are asking me.

Using CHtml class, the links will change automatically based on your configuration of the urlManager component where you can define your own rules. Rules are just mapping which says, if this controller/action is used, then generate this result. For example:

main.php (application/config/main.php)







Read this rule like: If in the CHtml class "message/goodbye" is used, then generate "[size="2"]good-bye-dear-customer/1[/size][size="2"]" link. [/size]

Now, when you put CHtml::link("Ciao", array("message/goodby", "id"=>1)) the folowing link will be generated:

<a href="[size="2"]good-bye-dear-customer/1[/size][size="2"]">Ciao</a>[/size]

[size="2"]Now you got pretty URLs.[/size]

The same thing applies when you call $this->createUrl() in the controller or in the view.

This means that one should always use CHtml class.

Hope I helped.