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.
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: