Yii 1.1: Front-end developer's guide


This wiki is a work in progress.

This wiki is targeted at developers that are new to Yii and are focusing on developing the front-end of a Yii application using languages such as HTML, CSS and JavaScript. Perhaps there are different developers working on the backend, but you are focused on the front-end. There are certain things you need to do to keep your code manageable for those backend developers. This article is meant to teach you those things.

Views ΒΆ

As a front-end developer, you will be focusing mainly on implementing views. Views are simply *.php files that contain mostly html which is sent to the browser. They may be embedded in layouts, which is very similar to a view itself. Layouts generally define the outside framework of a website, while the views define the content on the individual pages. View are contained in the protected/views directory, and are organized by controller (controllers are like categories that views with similar purposes reside - for instance a blog post list and blog post create view may be in a blog controller). Layouts are contained in the protected/views/layouts directory. Layouts may even be embedded in other layouts.

For instance, the view file for the front page is likely at /protected/views/site/index.php, as the front page is the index action in the site controller by default.

You can use the CHtml helper to render certain html elements. This should especially be done for links and forms. Please read this for code examples of CHtml.

You may find that explicitly a link such as:

<a href="/site/index">Home Page</a>

will only work if the application is installed on the root level of the server (for instance www.example.com). If the application was installed at www.example.com/foo, the above link would not work, as it would direct the user to www.example.com/site/index instead of www.example.com/foo/site/index. To write links that work independently of where the application is installed, one should use the CHtml::link() function.

To include a css file in say the /css directory, put this in the view:


Or for JS file in say the /js directory, put this in the view:


These code snippets will automatically generate the html inclusion in the HEAD section of the final html document to be returned to the user's browser

Here is more reading you may be interested in reading, depending on your interest and php level:

  2. Forms
  3. MVC Basics

Total 5 comments

#6142 report it
hobs at 2011/12/15 08:58am
Use CHtml and registerScriptFile guilt-free

Now that you call me on it, yea, PHP (Yii) isn't the bottleneck.

#6138 report it
hofrob at 2011/12/15 08:24am
I think you are confusing something

The speed of the execution of your PHP scripts definitely doesn't matter (That is the speed you were talking about). It is not the bottle neck. Using or not using the CHtml methods won't have any measurable effect.

There are other things to be concerned about if you really want to speed up your website.

#6133 report it
hobs at 2011/12/15 04:51am
Yii is Fast and Speed Matters

Just wondering, not complaining. I like Yii. I like frameworks. I like DRY. But speed is on my mind--I pay by the byte, and speed seems to affect SEO. Yii/php is faster than any other framework I've worked with.

#6130 report it
hofrob at 2011/12/15 02:45am
speed is not an issue

Why would you care about speed? Try to benchmark the difference.

When you use the CHtml (or as in my case another class that extends from CHtml) methods changes in this method effect the whole project. So you can easily customize your html links without going through every view.

The whole point of frameworks is that you don't have to repeat yourself. That's why you use these methods.

edit: jonah actually explains another very good reason (if not the main reason) to use CHtml::link: "To write links that work independently of where the application is installed, one should use the CHtml::link() function."

#6095 report it
hobs at 2011/12/12 05:53am

Nice summary of some features of Yii that help with views. I wonder what the speed/efficiency impact is for using CHtml::link or the app "register" functions, vs. hard-coding in HTML, or including hardcoded headers/footers in shared views like layouts.

Leave a comment

Please to leave your comment.

Write new article