Introduction to Yii
Framework
Tuan Nguyen
Web Developer at Tuoi Tre Online
nganhtuan63@gmail.com
Content

1.  Why I choose Yii Framework?

2.  Yii rich features. (Stuff I like)

3.  A practical folder structure for Web Application with Yii.

4.  Q&A.
1. Why I choose Yii Framework?
- Demand:

To build a robust and solid CMS. (Easy for both Dev., End-Users)

Previous Frameworks I worked with:

+ Zend Framework.
+ CakePHP.
+ MVC Framework made by myself.

Previous CMS I worked with:

+ Drupal
+ Wordpress
+ Other enterprise CMS.
1. Why I choose Yii Framework?
To get started, follow these links in order:

1. http://www.yiiframework.com/tour/ - Learn basic flow of creating new app.
2. http://www.yiiframework.com/doc/blog/ - Create a simple blog with Yii.
3. http://www.yiiframework.com/doc/guide/ - Learn features & components of
   Yii.

Advantages:
- Yii performance - Optimize with APC cache and Lazy Loading -
    http://www.yiiframework.com/performance/
- Very good and clear documents.
- Rich of modules and extensions.

Disadvantages:
- Forum discussions is not really active.
- Community is not as big as others.
Yii Rich Features
Features I usually work with:
  •  MVC Design Pattern and Request Handle Workflow.
  •  New Application Generator. (demo)
  •  Support multi-database systems (MySQL, SQLlite,...). (demo)
  •  URLManager (demo)
  •  Gii Generator: CRUD, Model, Form, Module,...your own generator. (demo)
  •  Controllers, Filters, AccessControl and Views (demo)
  •  Model - Working with database (CActiveRecord,...) - Rules and Validators. (demo)
  •  Form - Handle user input data - Rules and Validators (demo)
  •  Flexible OOP (Object-Oriented Programming). (demo)
----------------------------------------------------------------------------------------------------------------------
  •  Asset Manager
  •  Error Handler & Log Management
  •  Support multi-caching mechanism. (demo)
  •  Session and Cookie Management. (demo)
  •  Application Security (CSRF, Cookie validation,...). (demo)
  •  User Authentication with RBAC.
  •  Extending Yii - Extensions and Modules
  •  ...check more at: http://www.yiiframework.com/doc/guide/
Yii Rich Features
MVC Design Pattern and Request Handle Workflow

- http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc
Yii Rich Features
MVC Design Pattern and Request Handle Workflow

- http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc
Yii Rich Features
New Application Generator - Demo

- Run php yii/framework/yiic.php webapp /location
- Run and Test new application
Yii Rich Features
New Application Generator - Demo
Yii Rich Features
URLManager configuration

- Do not over-use URLManager due to downgrade of performance.
   (Using .htaccess as much as possible)
Yii Rich Features
Support multi-database systems (MySQL, SQLlite,...)

- Yii database is built on top of PDO. You can switch betweeb DBMS (MySQL,
    SQL,...) without the need to change application code

- Database configuration in protected/config/main.php
Yii Rich Features
Gii Generator: Model, Controller, CRUD,...Your Own generator

Demo:

- Config database connection first.
- Create new User Table in Database.
CREATE TABLE `yiisample`.`user` (
   `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
   `name` VARCHAR( 255 ) NOT NULL ,
   `email` VARCHAR( 255 ) NOT NULL ,
   `status` TINYINT( 2 ) NOT NULL DEFAULT '0'
) ENGINE = INNODB;


- Enable Gii in Applicaion
- Using Gii to generate code.
Yii Rich Features
Controllers, Filters, AccessControl and Views

Demo:

- Learn the basic workflow of controllers and views.
- Learn using filters with AccessControl.
- Learn more about the views:
    www.yiiframework.com/wiki/249/understanding-the-view-rendering-flow/
Yii Rich Features
Models (CActiveRecord, DAO...)

Demo CActiveRecord:

+ http://www.yiiframework.com/doc/guide/1.1/en/database.ar

- Using CActiveRecord to manage Table in Database.
- Features of CActiveRecord:

+ Rules and Validators
+ Behaviors and Events
+ Integreate data to Widgets, CActiveForm,...

Demo DAO:

+ http://www.yiiframework.com/doc/guide/1.1/en/database.dao
Yii Rich Features
Models - CActiveRecord vs DAO

CActiveRecord:
- Good for Inputting (rules, validators,...).
- Quick deploy thanks to Gii Generator.
- Easy to handle.

DAO:
- Less memory.
- Good for retrieving data.

Advice:
Caching and choose DAO to deploy application fast first. Implement by DAO
    later.
http://www.yiiframework.com/forum/index.php/topic/25825-dao-vs-activerecord-
    methods/
http://www.sheldmandu.com/php/php-mvc-frameworks/yii-dao-vs-active-record-
    performance
Yii Rich Features
Forms - CActiveForm

- It is based on Model Concept to collect user data.
- It support rules and validators like Model.
- Demo ContactForm with rules and validators
Yii Rich Features
Flexible Object-Oriented Programming (OOP)

- In Yii, you can extend any core class you want. Keep core code clean and
    logical.

- Demo with User Components.

To manage Users, Yii use 2 components/classes:

+ CWebUser : a component in application variable. It stores basic user
   information of current request (username, email, login by cookie).

+ CUserIdentity : a class to help user "Log in" to system. (Allow users to login
   by using File Data, Pre-Defined data or Database data,...)
Yii Rich Features
Flexible Object-Oriented Programming (OOP)
                                                CWebUser
User Login Workflow
                                                Login by Login Form
                      CUserIdentity will
  Login By            check login information   If identity is ok and no error, the
  Form                from user                 user will be truly logged in to
  User types his                                system by creating session for
  username and        You can implement         that user.
  password            core class here
                                                Login by Cookie

                                                If the cookie user send in request
                                                is ok (with some secure check).
                                                The User will be logged into the
                                                system like above.
  Login By
  Cookie
                                                You can implement core class
  User send its
                                                here
  cookie in the
  request
Yii Rich Features
Session and Cookie Management

- Yii supports Session and Cookie wrapper class.

Session:
- You can use Session Handler by File, Database or Memcache.

'session' => array(
               'class' => 'CDbHttpSession',
               'connectionID' => 'db',
               'autoCreateSessionTable'=>false,
               'sessionTableName'=>'gxc_session',
               'sessionName'=>'gxc_session_id' //Should Change for Different //Apps
 ),

You can implement/extend CDbHttpSession for your own use.
Yii Rich Features
Session and Cookie Management

Cookie:

- Writing to Cookie:
Yii::app()->request->cookies['cookie_name'] = new CHttpCookie('cookie_name', $value);


- Read from Cookie:
$cookie = Yii::app()->request->cookies['cookie_name']->value;


http://www.yiiframework.com/wiki/152/cookie-management-in-yii

Web User Cookie:
           'user'=>array(
                            'class'=>'cms.modules.user.components.GxcUser',

                                          'allowAutoLogin'=>true,
                                         'autoRenewCookie'=>true,
                                         'loginUrl'=>array('site/login'),
                                  'stateKeyPrefix'=>'gxc_u_', //Should Change for Different Apps
                            ),
Yii Rich Features
Application Security (CSRF, Cookie validation,...)

- Yii supports CSRF, Cookie Validation,...
- Learn more at:

http://www.yiiframework.com/doc/guide/1.1/en/topics.security

http://www.yiiframework.com/wiki/275/how-to-write-secure-yii-applications/
Yii Rich Features
Asset Managers

Demo:

- Using Asset Managers in Module to publish Asset Files. (Which we can't
   directly access due to .htaccess restrict)
- Demo with Rights module.
Yii Rich Features
Error Handler and Log Management

- Setup Error handler in config file.
- Log Management:

+ Write Log to File.
+ Display Log on Website.
+ Intergrate with PHP Quick Profiler extension.

http://www.yiiframework.com/extension/phpquickprofiler/
Yii Rich Features
Multi-Caching Mechanism

- Support: File Cache, Database Cache, APC Cache, Memcache,...Switch
   between easily.

http://www.yiiframework.com/doc/guide/1.1/en/caching.data
Yii Rich Features
User Authentication with RBAC.

- Yii supports RBAC for access control.
- In RBAC, there are 3 levels: Role, Task, Operations
- Popular extension to manage RBAC (Rights extension -
    www.yiiframework.com/extension/rights/ )
Yii Rich Features
Extending Yii - Extensions

Thanks to flexible Yii structure, you can easily extend Yii to:

+ Override core components.
+ Create Behaviors
+ Create Widgets
+ Create Module
+ ...More at

http://www.yiiframework.com/doc/guide/1.1/en/extension.overview
Practical Folder Structure
--apps
-----common
-----backend
-----frontend
-----console
--core
----yii

http://www.yiiframework.com/wiki/374/yiiboilerplate-setup-a-professional-
    project-structure-in-seconds/
Q&A
For more information or discussion:

- Nguyễn Anh Tuấn

- Email: nganhtuan63@gmail.com
- Website: http://nganhtuan.com
- Facebook: https://www.facebook.com/nganhtuan63

- Check my open source Yii CMS: http://www.gxccms.com
Thank you!
For more information or discussion:

- Nguyễn Anh Tuấn

- Email: nganhtuan63@gmail.com
- Website: http://nganhtuan.com
- Facebook: https://www.facebook.com/nganhtuan63

- Check my open source Yii CMS: http://www.gxccms.com

Introduction Yii Framework

  • 1.
    Introduction to Yii Framework TuanNguyen Web Developer at Tuoi Tre Online nganhtuan63@gmail.com
  • 2.
    Content 1.  Why Ichoose Yii Framework? 2.  Yii rich features. (Stuff I like) 3.  A practical folder structure for Web Application with Yii. 4.  Q&A.
  • 3.
    1. Why Ichoose Yii Framework? - Demand: To build a robust and solid CMS. (Easy for both Dev., End-Users) Previous Frameworks I worked with: + Zend Framework. + CakePHP. + MVC Framework made by myself. Previous CMS I worked with: + Drupal + Wordpress + Other enterprise CMS.
  • 4.
    1. Why Ichoose Yii Framework? To get started, follow these links in order: 1. http://www.yiiframework.com/tour/ - Learn basic flow of creating new app. 2. http://www.yiiframework.com/doc/blog/ - Create a simple blog with Yii. 3. http://www.yiiframework.com/doc/guide/ - Learn features & components of Yii. Advantages: - Yii performance - Optimize with APC cache and Lazy Loading - http://www.yiiframework.com/performance/ - Very good and clear documents. - Rich of modules and extensions. Disadvantages: - Forum discussions is not really active. - Community is not as big as others.
  • 5.
    Yii Rich Features FeaturesI usually work with: •  MVC Design Pattern and Request Handle Workflow. •  New Application Generator. (demo) •  Support multi-database systems (MySQL, SQLlite,...). (demo) •  URLManager (demo) •  Gii Generator: CRUD, Model, Form, Module,...your own generator. (demo) •  Controllers, Filters, AccessControl and Views (demo) •  Model - Working with database (CActiveRecord,...) - Rules and Validators. (demo) •  Form - Handle user input data - Rules and Validators (demo) •  Flexible OOP (Object-Oriented Programming). (demo) ---------------------------------------------------------------------------------------------------------------------- •  Asset Manager •  Error Handler & Log Management •  Support multi-caching mechanism. (demo) •  Session and Cookie Management. (demo) •  Application Security (CSRF, Cookie validation,...). (demo) •  User Authentication with RBAC. •  Extending Yii - Extensions and Modules •  ...check more at: http://www.yiiframework.com/doc/guide/
  • 6.
    Yii Rich Features MVCDesign Pattern and Request Handle Workflow - http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc
  • 7.
    Yii Rich Features MVCDesign Pattern and Request Handle Workflow - http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc
  • 8.
    Yii Rich Features NewApplication Generator - Demo - Run php yii/framework/yiic.php webapp /location - Run and Test new application
  • 9.
    Yii Rich Features NewApplication Generator - Demo
  • 10.
    Yii Rich Features URLManagerconfiguration - Do not over-use URLManager due to downgrade of performance. (Using .htaccess as much as possible)
  • 11.
    Yii Rich Features Supportmulti-database systems (MySQL, SQLlite,...) - Yii database is built on top of PDO. You can switch betweeb DBMS (MySQL, SQL,...) without the need to change application code - Database configuration in protected/config/main.php
  • 12.
    Yii Rich Features GiiGenerator: Model, Controller, CRUD,...Your Own generator Demo: - Config database connection first. - Create new User Table in Database. CREATE TABLE `yiisample`.`user` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 255 ) NOT NULL , `email` VARCHAR( 255 ) NOT NULL , `status` TINYINT( 2 ) NOT NULL DEFAULT '0' ) ENGINE = INNODB; - Enable Gii in Applicaion - Using Gii to generate code.
  • 13.
    Yii Rich Features Controllers,Filters, AccessControl and Views Demo: - Learn the basic workflow of controllers and views. - Learn using filters with AccessControl. - Learn more about the views: www.yiiframework.com/wiki/249/understanding-the-view-rendering-flow/
  • 14.
    Yii Rich Features Models(CActiveRecord, DAO...) Demo CActiveRecord: + http://www.yiiframework.com/doc/guide/1.1/en/database.ar - Using CActiveRecord to manage Table in Database. - Features of CActiveRecord: + Rules and Validators + Behaviors and Events + Integreate data to Widgets, CActiveForm,... Demo DAO: + http://www.yiiframework.com/doc/guide/1.1/en/database.dao
  • 15.
    Yii Rich Features Models- CActiveRecord vs DAO CActiveRecord: - Good for Inputting (rules, validators,...). - Quick deploy thanks to Gii Generator. - Easy to handle. DAO: - Less memory. - Good for retrieving data. Advice: Caching and choose DAO to deploy application fast first. Implement by DAO later. http://www.yiiframework.com/forum/index.php/topic/25825-dao-vs-activerecord- methods/ http://www.sheldmandu.com/php/php-mvc-frameworks/yii-dao-vs-active-record- performance
  • 16.
    Yii Rich Features Forms- CActiveForm - It is based on Model Concept to collect user data. - It support rules and validators like Model. - Demo ContactForm with rules and validators
  • 17.
    Yii Rich Features FlexibleObject-Oriented Programming (OOP) - In Yii, you can extend any core class you want. Keep core code clean and logical. - Demo with User Components. To manage Users, Yii use 2 components/classes: + CWebUser : a component in application variable. It stores basic user information of current request (username, email, login by cookie). + CUserIdentity : a class to help user "Log in" to system. (Allow users to login by using File Data, Pre-Defined data or Database data,...)
  • 18.
    Yii Rich Features FlexibleObject-Oriented Programming (OOP) CWebUser User Login Workflow Login by Login Form CUserIdentity will Login By check login information If identity is ok and no error, the Form from user user will be truly logged in to User types his system by creating session for username and You can implement that user. password core class here Login by Cookie If the cookie user send in request is ok (with some secure check). The User will be logged into the system like above. Login By Cookie You can implement core class User send its here cookie in the request
  • 19.
    Yii Rich Features Sessionand Cookie Management - Yii supports Session and Cookie wrapper class. Session: - You can use Session Handler by File, Database or Memcache. 'session' => array( 'class' => 'CDbHttpSession', 'connectionID' => 'db', 'autoCreateSessionTable'=>false, 'sessionTableName'=>'gxc_session', 'sessionName'=>'gxc_session_id' //Should Change for Different //Apps ), You can implement/extend CDbHttpSession for your own use.
  • 20.
    Yii Rich Features Sessionand Cookie Management Cookie: - Writing to Cookie: Yii::app()->request->cookies['cookie_name'] = new CHttpCookie('cookie_name', $value); - Read from Cookie: $cookie = Yii::app()->request->cookies['cookie_name']->value; http://www.yiiframework.com/wiki/152/cookie-management-in-yii Web User Cookie: 'user'=>array( 'class'=>'cms.modules.user.components.GxcUser', 'allowAutoLogin'=>true, 'autoRenewCookie'=>true, 'loginUrl'=>array('site/login'), 'stateKeyPrefix'=>'gxc_u_', //Should Change for Different Apps ),
  • 21.
    Yii Rich Features ApplicationSecurity (CSRF, Cookie validation,...) - Yii supports CSRF, Cookie Validation,... - Learn more at: http://www.yiiframework.com/doc/guide/1.1/en/topics.security http://www.yiiframework.com/wiki/275/how-to-write-secure-yii-applications/
  • 22.
    Yii Rich Features AssetManagers Demo: - Using Asset Managers in Module to publish Asset Files. (Which we can't directly access due to .htaccess restrict) - Demo with Rights module.
  • 23.
    Yii Rich Features ErrorHandler and Log Management - Setup Error handler in config file. - Log Management: + Write Log to File. + Display Log on Website. + Intergrate with PHP Quick Profiler extension. http://www.yiiframework.com/extension/phpquickprofiler/
  • 24.
    Yii Rich Features Multi-CachingMechanism - Support: File Cache, Database Cache, APC Cache, Memcache,...Switch between easily. http://www.yiiframework.com/doc/guide/1.1/en/caching.data
  • 25.
    Yii Rich Features UserAuthentication with RBAC. - Yii supports RBAC for access control. - In RBAC, there are 3 levels: Role, Task, Operations - Popular extension to manage RBAC (Rights extension - www.yiiframework.com/extension/rights/ )
  • 26.
    Yii Rich Features ExtendingYii - Extensions Thanks to flexible Yii structure, you can easily extend Yii to: + Override core components. + Create Behaviors + Create Widgets + Create Module + ...More at http://www.yiiframework.com/doc/guide/1.1/en/extension.overview
  • 27.
  • 28.
    Q&A For more informationor discussion: - Nguyễn Anh Tuấn - Email: nganhtuan63@gmail.com - Website: http://nganhtuan.com - Facebook: https://www.facebook.com/nganhtuan63 - Check my open source Yii CMS: http://www.gxccms.com
  • 29.
    Thank you! For moreinformation or discussion: - Nguyễn Anh Tuấn - Email: nganhtuan63@gmail.com - Website: http://nganhtuan.com - Facebook: https://www.facebook.com/nganhtuan63 - Check my open source Yii CMS: http://www.gxccms.com