You are on page 1of 29

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.

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

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

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

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

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

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

Demo . Yii Rich Features New Application Generator .

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

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

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

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

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

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

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

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

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 . password core class here Login by Cookie If the cookie user send in request is ok (with some secure check). 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.

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

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

Learn more at: http://www. . Cookie Validation. Yii Rich Features Application Security (CSRF.) .security http://www. Cookie ..Yii supports CSRF...yiiframework..yiiframework.

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

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

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

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

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

yiiframework. Practical Folder Structure --apps -----common -----backend -----frontend -----console --core ----yii project-structure-in-seconds/ .

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

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