Yii Framework - introduction and more

Yii Framework - introduction and more



Yii is a high-performance PHP framework best for developing Web 2.0 applications.

Yii comes with rich features: MVC, DAO/ActiveRecord, I18N/L10N, caching, authentication and role-based access control, scaffolding, testing, etc. It can reduce your development time significantly.


Yii supplies an easy way to know what kind of modules and such are required to use its power.

Requirements are available at http://your-domain/path/to/yii/requirements/ and should look like the following: 

Installation and a tour

Yii does not require installation, yii documentation provides a well explained 'take a tour' page which let you generate your first 'hello world' page.

How to

So the question is how to do what? There are two topics which I want to write about today:

  1. Layouts
  2. Assets Manager


Layouts are usually an important feature which should be a part of (almost) any MVC framework.

Yii framework provides us a easy way to use layouts:

  1. Create new .php file in  protected/views/layouts/your_new_layout.php
  2. Add your relevant data, while \$content reflects the rendered data:

<?php echo $content ?>
  1. Change your layout by requirements for each controller action or forthe entire controller by using:

$this->layout = 'your_new_layout';
//Should be look like this:
function actionIndex(){
    $this->layout = 'your_new_layout';

That's it! easy, ha ?

Assets Manager

Yii framework provides a very nice way to handle assets. The thing which I like the most is where do I put my assets declarations.

I saw many ways to declare about assets:

  • Part of the controller
  • Part of the HTML (inline)
  • Both
  • Weird ways which I should not even mention

In my perspective, assets declaration should be part of the HTML by using PHP (or any other template system\language), for example:

//load scripts
$clientScript = Yii::app()->getClientScript();
//load css
More HTML here

As you can see, I declared both JS and CSS in my HTML file , so why ?

As I see it the developers which should maintain the way we use our assets are the front-end developers.

Implementing the above code won't be enough when working on a live environment, therefor we have to solve it by using JS and CSS minifiers. This guide might not bring the best way to do that since I just want to prove my point, so please share your ways to do that.

To minify my assets I'v used  DynamicRes extension with the following steps:

  1. Download the extension and install it into protected/extension/
  2. Add the following to protected/config/main.php:

// application components
//@see http://www.yiiframework.com/extension/dynamicres/
            'class' => 'ext.DynamicRes.DynamicRes',
            'urlConfig' => array( // Its fix Css, and convert Url to RealName
                'baseUrl'  => '/', // Url of your Site (ending with /), modify it if you use subdomain
                'basePath' => dirname(__FILE__).'/../../', // path of your site (ending with /) (No Change This)
  1. Replace Yii::app()->getClientScript()with Yii::app()->dynamicRes
  2. You may add your env param to make your structure more dynamic

My final implementation looks like this:

* ENV Settings
* TODO: should be move to Yii::app() to return ClientScript by ENV settings
if (Yii::app()->params['env'] == 'dev'){
    $baseUrl = Yii::app()->request->baseUrl;
    $cs = Yii::app()->getClientScript();
} else {
    $baseUrl = '';
    $cs = Yii::app()->dynamicRes;
//load it from google cache so no need to pack and minify
$cs->registerScriptFile($baseUrl . '/js/external/underscore.js'            ,CClientScript::POS_END);
$cs->registerScriptFile($baseUrl . '/js/external/Backbone.js'          ,CClientScript::POS_END);


Yii framework is an interesting toolbox which I ll continue to explore.

In my upcoming posts I ll write about memcache, modules and more.

Feel free to write\suggest\ask anything that comes in mind.



Work In Progress 🚧