Introduction to Yii

Tuan Nguyen
Web Developer at Tuoi Tre Online


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.

End-Users) Previous Frameworks I worked with: + Zend Framework.Demand: To build a robust and solid CMS. 1. + MVC Framework made by myself. Previous CMS I worked with: + Drupal + Wordpress + Other enterprise CMS. Why I choose Yii Framework? . (Easy for both Dev. . + CakePHP..

http://www. Advantages: .Rich of modules and extensions. http://www.Learn features & components of Yii.yiiframework. . follow these links in order: 1. 2. .Very good and clear documents.Create a simple blog with Yii.Yii performance .Learn basic flow of creating new app.Forum discussions is not really active.Community is not as big as others. . .Optimize with APC cache and Lazy Loading - http://www. Disadvantages: .com/performance/ . .com/doc/guide/ . 3. Why I choose Yii Framework? To get started.yiiframework.yiiframework.

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

http://www. Yii Rich Features MVC Design Pattern and Request Handle Workflow .

yiiframework.1/en/ . Yii Rich Features MVC Design Pattern and Request Handle Workflow .http://www.

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

Yii Rich Features New Application Generator .Demo .

Do not over-use URLManager due to downgrade of performance.htaccess as much as possible) . (Using . Yii Rich Features URLManager configuration .

.. Yii Rich Features Support multi-database systems (MySQL.Database configuration in protected/config/main. SQL.php .) without the need to change application code .Yii database is built on top of PDO.. You can switch betweeb DBMS (MySQL....) . SQLlite.

Using Gii to generate code. . `status` TINYINT( 2 ) NOT NULL DEFAULT '0' ) ENGINE = INNODB.. Controller. .Config database connection first. . `name` VARCHAR( 255 ) NOT NULL . CREATE TABLE `yiisample`.Enable Gii in Applicaion . CRUD.. `email` VARCHAR( 255 ) NOT NULL . Yii Rich Features Gii Generator: Model..Create new User Table in Database.Your Own generator Demo: .`user` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY .

yiiframework. AccessControl and Views Demo: . Filters. .Learn more about the views: www. Yii Rich Features Controllers. .Learn using filters with AccessControl.Learn the basic workflow of controllers and .

com/doc/guide/ .yiiframework..Features of CActiveRecord: + Rules and Validators + Behaviors and Events + Integreate data to Widgets.yiiframework. CActiveForm.1/en/database. Yii Rich Features Models (CActiveRecord..Using CActiveRecord to manage Table in Database.1/en/database. .. Demo DAO: + http://www.dao .) Demo CActiveRecord: + DAO..

Easy to handle..Less memory.). DAO: .yiiframework. . Implement by DAO later. .Good for retrieving data. validators.php/topic/25825-dao-vs-activerecord- methods/ http://www.Good for Inputting (rules. Advice: Caching and choose DAO to deploy application fast performance .com/forum/index..CActiveRecord vs DAO CActiveRecord: . Yii Rich Features Models .sheldmandu.Quick deploy thanks to Gii Generator.. . http://www.

Yii Rich Features Forms .Demo ContactForm with rules and validators .It is based on Model Concept to collect user data.It support rules and validators like Model. . .CActiveForm .

Demo with User Components. . email. Pre-Defined data or Database data. you can extend any core class you want. To manage Users.. Keep core code clean and logical. login by cookie). (Allow users to login by using File Data. Yii use 2 components/classes: + CWebUser : a component in application variable.In Yii. Yii Rich Features Flexible Object-Oriented Programming (OOP) .. It stores basic user information of current request (username..) . + CUserIdentity : a class to help user "Log in" to system.

The User will be logged into the system like above. 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. Login By Cookie You can implement core class User send its here cookie in the request . password core class here Login by Cookie If the cookie user send in request is ok (with some secure check). 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.

'autoCreateSessionTable'=>false.You can use Session Handler by File. . 'connectionID' => 'db'. Database or Memcache. Session: . 'session' => array( 'class' => 'CDbHttpSession'.Yii supports Session and Cookie wrapper class. You can implement/extend CDbHttpSession for your own use. Yii Rich Features Session and Cookie Management . 'sessionName'=>'gxc_session_id' //Should Change for Different //Apps ). 'sessionTableName'=>'gxc_session'.

$value).Writing to Cookie: Yii::app()->request->cookies['cookie_name'] = new CHttpCookie('cookie_name' Web User Cookie: 'user'=>array( 'class'=>'cms.components.user.Read from Cookie: $cookie = Yii::app()->request->cookies['cookie_name']->value. 'autoRenewCookie'=>true. //Should Change for Different Apps ). 'stateKeyPrefix'=>'gxc_u_'. 'allowAutoLogin'=>true.yiiframework. 'loginUrl'=>array('site/login'). Yii Rich Features Session and Cookie Management Cookie: . . . http://www.GxcUser'.

Yii supports CSRF. Yii Rich Features Application Security ( .Learn more at: http://www. Cookie Validation.yiiframework. .com/doc/guide/1..) .yiiframework.... Cookie validation..1/en/topics.

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

http://www.yiiframework. Yii Rich Features Error Handler and Log Management . + Display Log on Website. .com/extension/phpquickprofiler/ . + Intergrate with PHP Quick Profiler extension.Log Management: + Write Log to File.Setup Error handler in config file.

com/doc/guide/1. Memcache. Yii Rich Features Multi-Caching Mechanism . APC Cache.Support: File Cache.. http://www..Switch between .1/en/caching.yiiframework.. Database Cache.

In ) .yiiframework.Popular extension to manage RBAC (Rights extension - www. there are 3 levels: Role. . Task. Operations . .Yii supports RBAC for access control. Yii Rich Features User Authentication with RBAC.

.More at http://www.Extensions Thanks to flexible Yii structure. you can easily extend Yii to: + Override core components.1/en/extension.overview . + Create Behaviors + Create Widgets + Create Module + . Yii Rich Features Extending Yii .com/doc/guide/1..yiiframework.

com/wiki/374/yiiboilerplate-setup-a-professional- project-structure-in-seconds/ . Practical Folder Structure --apps -----common -----backend -----frontend -----console --core ----yii http://www.yiiframework.

com .Website: .com/nganhtuan63 .Nguyễn Anh Tuấn .Check my open source Yii CMS: http://www.Email: nganhtuan63@gmail. Q&A For more information or discussion: .gxccms.Facebook: https://www.

Email: .com . Thank you! For more information or discussion: .Nguyễn Anh Tuấn .com .gxccms.facebook.Facebook: https://www.Check my open source Yii CMS: .Website: http://nganhtuan.